Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kandepet committed May 26, 2017
0 parents commit 9dada26
Show file tree
Hide file tree
Showing 19 changed files with 1,409 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
katana.o: src/katana.cc src/datastructures.h src/config.h src/stl.h \
src/gcode.h src/slicer.h src/katana.h src/debug.h src/infill.h
config.o: src/config.cc src/config.h
stl.o: src/stl.cc src/datastructures.h src/config.h src/katana.h \
src/debug.h src/slicer.h src/infill.h src/gcode.h src/stl.h
gcode.o: src/gcode.cc src/datastructures.h src/config.h src/katana.h \
src/debug.h src/slicer.h src/infill.h src/gcode.h src/stl.h
slicer.o: src/slicer.cc src/datastructures.h src/config.h src/stl.h \
src/gcode.h src/slicer.h src/katana.h src/debug.h src/infill.h
infill.o: src/infill.cc src/datastructures.h src/config.h src/stl.h \
src/katana.h src/debug.h src/slicer.h src/infill.h src/gcode.h
katana.o: src/katana.h src/config.h src/datastructures.h src/debug.h \
src/slicer.h src/infill.h src/gcode.h src/stl.h
config.o: src/config.h
stl.o: src/stl.h src/datastructures.h
gcode.o: src/gcode.h src/datastructures.h
slicer.o: src/slicer.h src/datastructures.h
infill.o: src/infill.h src/datastructures.h
datastructures.o: src/datastructures.h
debug.o: src/debug.h
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CXX = c++
CXX_FLAGS = -std=c++11 -Wfatal-errors -Wall

BIN = katana
# Put all auto generated stuff to this build dir.
BUILD_DIR = ./build

# List of all .cpp source files.
SOURCE = $(wildcard src/*.cc)

# All .o files go to build dir.
OBJ = $(SOURCE:%.cc=$(BUILD_DIR)/%.o)
# Gcc/Clang will create these .d files containing dependencies.
DEP = $(OBJ:%.o=%.d)

# Default target named after the binary.
$(BIN) : $(BUILD_DIR)/$(BIN)

debug: CXX_FLAGS += -DDEBUG_ENABLED -g
debug: $(BIN)

# Actual target of the binary - depends on all .o files.
$(BUILD_DIR)/$(BIN) : $(OBJ)
@mkdir -p $(@D)
$(CXX) $(CXX_FLAGS) $^ -o $@

# Include all .d files
-include $(DEP)

# Build target for every single object file.
# The potential dependency on header files is covered
# by calling `-include $(DEP)`.
$(BUILD_DIR)/%.o : %.cc
@mkdir -p $(@D)
$(CXX) $(CXX_FLAGS) -MMD -c $< -o $@

.PHONY : clean
clean :
-rm $(BUILD_DIR)/$(BIN) $(OBJ) $(DEP)
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Katana
=========

An experimental stl slicer and gcode generator written in C++. This code is based on schlizzer, written by Paul Geisler.

Usage:
./katana inputfile.stl outputfile.gcode


Important features missing in respect to Slic3r:

- No discrimination of 'solid' and 'fill' areas. They are handled equally with fill_density=1.
- Only 'rectilinear' fill pattern supported.
- No contour correction of perimeter and infill, so the object exceeds the specified .stl
- Bad route planning leading to bad movement order with useless many and long travels
- Even worse planning for 'non manifold' objects (most of thingyverse i guess..)
- No brim, skirt, cooling etc.
- No automatic placement and z leveling
- Generated Gcode need to be extended before printing
10 changes: 10 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
start_gcode = M107\nM190 S80 ; wait for bed temperature to be reached\nM104 S230 ; set temperature\nG28\nG1 Z27 F200\nG1 X70 F2000\nG91\nG90\nG1 Y30 F2000\nG1 Z0 F200\nG92 E0\n//prime extruder\nG1 F100.0 E2.00\nM109 S230 ; wait for temperature to be reached\nG90 ; use absolute coordinates\nG21 ; set units to millimeters\nG92 E0\nM82 ; use absolute distances for extrusion\n

end_gcode = M107\nM104 S0 ; turn off temperature\nM140 S0\nG91\nG1 E-2\nG1 Z10 F200\nG1 Y190 F1000\nG90\nM84 ; disable motors\n
filament_diameter = 2.9
nozzle_diameter = 0.7
layer_height = 0.3
extrusion_multiplier = 0.6
retract_before_travel = 2
retract_length = 1
z_offset = -.7
62 changes: 62 additions & 0 deletions src/config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

#include <stdio.h>
#include <assert.h>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <array>
#include <math.h>
#include <exception>
#include <fstream>

#include "config.h"

// read a config value
float Config::get(const char* parameter){
// ensure sure the value was set by the config file
assert(this->config.count(parameter)==1);

return this->config[parameter];
}

const char* Config::getString(const char* parameter){
// ensure sure the value was set by the config file
assert(this->configString.count(parameter)==1);

return this->configString[parameter].c_str();
}

// load config file in Slic3r format
void Config::loadConfig(const char* filename){
printf("Loading config %s...\n",filename);
FILE* file=fopen(filename,"r");
char line[256];
while(!feof(file)){
if(fgets(line, sizeof(line), file)){
char name[256];
float value;
char valueString[1024];
// scan for one 'name = value' entry
// store single float values
int found=sscanf(line,"%s = %e",name, &value);
if(found)
this->config[name]=value;
// also store value as string, useful for strings and other non-float parameters
found=sscanf(line,"%s = %[^\n]",name, valueString);
if(found){
int pos;
std::string s=valueString;
// replace \n by real newlines
std::string newlineEscape="\\n";
while ((pos = s.find(newlineEscape))!=-1) {
s.erase(pos, newlineEscape.length());
s.insert(pos, "\n");
}
this->configString[name]=s;
}
}
}
fclose(file);
}

19 changes: 19 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __CONFIG_H__
#define __CONFIG_H__

class Config {
public:
float get(const char* parameter);
const char* getString(const char* parameter);

void loadConfig(const char* filename);

private:
// a map holding configuration values read from config.ini
std::map<std::string, float> config;
std::map<std::string, std::string> configString;
const char* configFileName;

};

#endif //__CONFIG_H__
143 changes: 143 additions & 0 deletions src/datastructures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#ifndef __DATASTRUSTURES_H__
#define __DATASTRUSTURES_H__

#include <iostream>
#include <sstream>

// data structures and operations

// a 3d vertex
struct Vertex{
float x,y,z;

bool inline operator==(const Vertex& b) const {
return this->x==b.x && this->y==b.y && this->z==b.z;
}

bool inline operator!=(Vertex& b) const {
return !(*this==b);
}

// dot product
float inline dot(const Vertex& b) const {
return this->x*b.x+this->y*b.y+this->z*b.z;
}

// distance of two vertices
float inline distance(Vertex& b) const {
float dx=this->x-b.x, dy=this->y-b.y, dz=this->z-b.z;
return sqrt(dx*dx+dy*dy+dz*dz);
}

float inline length() const {
return sqrt(this->dot(*this));
}

Vertex inline operator*(const float a) const
{
Vertex r={a*this->x,a*this->y,a*this->z};
return r;
}

Vertex inline operator+(const Vertex& b) const
{
Vertex r={this->x+b.x,this->y+b.y,this->z+b.z};
return r;
}

Vertex inline operator-(const Vertex& b) const
{
return *this+(b*-1.f);
}

Vertex inline normalize() const {
float l=this->length();
if(l==0) return (Vertex){0,0,0};
return (*this)*(1.f/l);
}

// simple half ordering in z,y,x order
// automatically used by orderes containers like std::set, std::map
bool inline operator<(const Vertex& b) const
{
return
(this->z < b.z) ||
((this->z==b.z) && (this->y< b.y)) ||
((this->z==b.z) && (this->y==b.y) && (this->x<b.x));
}
};


// directed order for sorting vertices along a given 3d direction
struct VertexSweepOrder
{
Vertex dir;
VertexSweepOrder(Vertex _dir){
dir=_dir;
}

bool operator() (const Vertex& a, const Vertex& b) const
{
return a.dot(dir)<b.dot(dir);
}
};


// a triangle referencing three vertices
struct Triangle
{
Vertex* vertices[3];
Vertex normal;

void inline sortTriangleVertices()
{
Vertex** vs=this->vertices;
if(vs[0]->z > vs[1]->z) std::swap(vs[0],vs[1]);
if(vs[0]->z > vs[2]->z) std::swap(vs[0],vs[2]);
if(vs[1]->z > vs[2]->z) std::swap(vs[1],vs[2]);
}
};


// helper struct for sorting vertices by some value
struct VertexIndex
{
float value;
Triangle* triangle;

bool inline operator<(const VertexIndex& b) const
{
return this->value<b.value;
};
};


// a line segment
// usually resulting of the intersection from a triangle with a z plane
struct Segment
{
std::array<Vertex,2> vertices; // the two endpoints of this segment

// these are used temporary and are invalidated later. do not use them:
std::array<Segment*,2> neighbours; // pointers to this segment adjacent ones

long orderIndex; // an index generated to order segments for efficient printing

Vertex normal; // segment line normal

bool inline operator<(const Segment& b) const {
return this->orderIndex<b.orderIndex;
}
};


// a layer holding the segments build by intersecting the mesh with a z plane
struct Layer
{

float z; // z plane
std::vector<Triangle*> triangles; // triangles touching this layer
std::vector<Segment> segments; // segments generated for printing
};

#endif //__DATASTRUSTURES_H__
72 changes: 72 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef __DEBUG_H__
#define __DEBUG_H__

#include <iostream>
#include <sstream>

#include <cstdarg> /* va_end(), va_list, va_start(), vprintf() */
#include <cstdio> /* vprintf() */

// Buffered debug output
// TODO: flush buffer after buffering X characters.
class DebugOut {
public:
static DebugOut & Instance()
{
// Since it's a static variable, if the class has already been created,
// It won't be created again.
// And it **is** thread-safe in C++11.

static DebugOut myInstance;

// Return a reference to our instance.
return myInstance;
}

// delete copy and move constructors and assign operators
DebugOut(DebugOut const&) = delete; // Copy construct
DebugOut(DebugOut&&) = delete; // Move construct
DebugOut& operator=(DebugOut const&) = delete; // Copy assign
DebugOut& operator=(DebugOut &&) = delete; // Move assign

protected:
DebugOut(std::ostream& out = std::cout) : m_Out(out) {}
~DebugOut() {
m_Stream << "\n";
m_Out << m_Stream.rdbuf();
m_Out.flush();
}
public:

// cout style debug print function.
template <class T>
DebugOut& operator<<(const T& thing) { m_Stream << thing; return *this; }

// printf style debug print function
DebugOut& printf(const char *filename, int lineNo, const char* funcName, const char *fmt...) {
m_Stream << filename << ":" << lineNo << " (" << funcName << ") ";
char buffer[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, 1024, fmt, ap);
va_end(ap);
m_Stream << buffer;
return *this;
}
private:
std::stringstream m_Stream;
std::ostream& m_Out;
};


#ifndef DEBUG_ENABLED
#define DEBUG(_) do {} while(0)
#define DPRINTF(format, args...) do {} while(0)
#define XTRACE
#else
#define DEBUG(Message_) DebugOut::Instance() << __FILE__ << ":" << __LINE__ << " (" << __PRETTY_FUNCTION__ << ") " << Message_
#define DPRINTF(format, ...) DebugOut::Instance().printf(__FILE__, __LINE__, __PRETTY_FUNCTION__, format, ##__VA_ARGS__)
#endif


#endif //__DEBUG_H__
Loading

0 comments on commit 9dada26

Please sign in to comment.