// with file void Cal::getLines(const char *name_file) { std::ifstream fd (name_file); std::string line; if (fd.is_open()) { while (std::getline (fd,line)) { if (line == ";;" || line == "^D") break; this->_lines->push_back(line); } if (this->_lines->size() == 0) { std::stringstream ss; ss << "Error: instructions not found."; throw ft_exception(ss.str()); } fd.close(); scanLines(); exeLines(); } else { std::stringstream ss; ss << "Error: open " << name_file << "."; throw ft_exception(ss.str()); } }
// without file void Cal::getLines() { std::string line; while (std::getline (std::cin,line)) { if (line == ";;" || line == "^D") break; this->_lines->push_back(line); } if (this->_lines->size() == 0) { std::stringstream ss; ss << "Error: instructions not found."; throw ft_exception(ss.str()); } scanLines(); exeLines(); }
void GroundPlaneGenerator::scanImage(QImage & image, double bWidth, double bHeight, double pixelFactor, double res, const QString & colorString, bool makeConnector, bool makeOffset, QSizeF minAreaInches, double minDimensionInches, QPointF polygonOffset) { QList<QRect> rects; scanLines(image, bWidth, bHeight, rects); QList< QList<int> * > pieces; splitScanLines(rects, pieces); foreach (QList<int> * piece, pieces) { QList<QPolygon> polygons; QList<QRect> newRects; foreach (int i, *piece) { QRect r = rects.at(i); newRects.append(QRect(r.x() * pixelFactor, r.y() * pixelFactor, (r.width() * pixelFactor) + 1, pixelFactor + 1)); // + 1 is for off-by-one converting rects to polys } // note: there is always one joinScanLines(newRects, polygons); QPointF offset; QString pSvg = makePolySvg(polygons, res, bWidth, bHeight, pixelFactor, colorString, makeConnector, makeOffset ? &offset : NULL, minAreaInches, minDimensionInches, polygonOffset); if (pSvg.isEmpty()) continue; m_newSVGs.append(pSvg); if (makeOffset) { offset *= FSvgRenderer::printerScale(); m_newOffsets.append(offset); // offset now in pixels } /* QFile file4("testPoly.svg"); file4.open(QIODevice::WriteOnly); QTextStream out4(&file4); out4 << pSvg; file4.close(); */ }
int main(int argc, char* argv[]) { msgbuf = malloc(256); msgbuf[0] = 0; currentLayer = 0; cache = true; int longIndex; int opt; do { opt = getopt_long(argc, argv, optString, longOpts, &longIndex); if (opt != -1) { switch( opt ) { case 'l': currentLayer = strtol(optarg, NULL, 10); break; case 'w': extrusionWidth = strtof(optarg, NULL); break; case 'n': printf("DISABLING CACHE\n"); cache = false; break; case 'h': /* fall-through is intentional */ case '?': display_usage(); break; case 0: /* long option without a short arg */ //if( strcmp( "randomize", longOpts[longIndex].name ) == 0 ) { // globalArgs.randomized = 1; //} break; default: /* You won't actually get here. */ break; } } } while (opt != -1); if (optind >= argc) display_usage(); int fd = open(argv[optind], 0); if (fd == -1) die("Open ", argv[optind]); struct stat filestats; if (fstat(fd, &filestats) == -1) die("fstat ", argv[optind]); filesz = filestats.st_size; printf("File is %d long\n", filesz); #ifdef __linux__ gcodefile = mmap(NULL, filesz, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0); #elif defined __APPLE__ gcodefile = mmap(NULL, filesz, PROT_READ, MAP_PRIVATE, fd, 0); #else #error "don't know how to mmap on this system!" #endif if (gcodefile == MAP_FAILED) die("mmap ", argv[optind]); gcodefile_end = &gcodefile[filesz]; busy = BUSY_SCANFILE; scanLines(); if (currentLayer >= layerCount) currentLayer = layerCount - 1; //for (int i = 0; i < layerCount; i++) // printf("Layer %3d starts at %7d and is %7d bytes long\n", i, layer[i].index - gcodefile, layer[i].size); Running = true; Surf_Display = NULL; if (SDL_Init(SDL_INIT_EVERYTHING) < 0) die("SDL_init", ""); if (FcInitLoadConfigAndFonts() == ((void *) FcTrue)) die("FontConfig Init",""); // from http://www.spinics.net/lists/font-config/msg03050.html FcPattern *pat, *match; FcResult result; char *file; int index; pat = FcPatternCreate(); FcPatternAddString(pat, FC_FAMILY, (FcChar8 *) "Mono"); FcConfigSubstitute(NULL, pat, FcMatchPattern); FcDefaultSubstitute(pat); match = FcFontMatch(NULL, pat, &result); FcPatternGetString(match, FC_FILE, 0, (FcChar8 **) &file); FcPatternGetInteger(match, FC_INDEX, 0, &index); font = ftglCreateExtrudeFont(file); if (!font) die("FTGL createFont", ""); FcPatternDestroy (match); FcPatternDestroy (pat); #ifdef OPENGL transX = transY = 0.0; zoomFactor = 1.0; resize(600, 600); #else viewPortL = viewPortT = 0.0; viewPortR = viewPortB = 200.0; zoomFactor = 3.0; resize(viewPortR * zoomFactor, viewPortB * zoomFactor); #endif SDL_WM_SetCaption("gcodeview", 0); drawLayer(currentLayer); layerVelocity = 0; timerIdle = SDL_AddTimer(20, &timerCallback, (void *) TIMER_IDLE); SDL_Event Event; while(Running != false) { if (busy) { Event.type = SDL_NOEVENT; SDL_PollEvent(&Event); } else { if (SDL_WaitEvent(&Event) == 0) die("SDL_WaitEvent", ""); } //SDL_RemoveTimer(timerIdle); switch (Event.type) { case SDL_NOEVENT: if (busy & BUSY_SCANFILE) { // TODO: scan next layer scanLine(); if ((busy & BUSY_SCANFILE) == 0) { if (cache) { printf("File scanned, rendering...\n"); busy = BUSY_RENDER; } else { printf("File scanned.\n"); busy = 0; } } } else if ((busy & BUSY_RENDER) && cache) { bool allRendered = true; int i; // TODO: render next layer in background for (i = 0; i < layerCount; i++) { if (layer[i].glList == 0) { layer[i].glList = glGenLists(1); glNewList(layer[i].glList, GL_COMPILE); glBegin(GL_QUADS); for (int j = SHADOW_LAYERS; j >= 1; j--) { if (i - j > 0) render_layer(i - j, SHADOW_ALPHA - (j - 1) * (SHADOW_ALPHA / SHADOW_LAYERS)); } render_layer(i, 1.0); glEnd(); glEndList(); layer[i].flags |= LD_LISTGENERATED; allRendered = false; break; } } if (allRendered) { printf("All %d layers rendered\n", i); busy &= ~BUSY_RENDER; } } break; case SDL_QUIT: Running = false; break; case SDL_VIDEORESIZE: resize(Event.resize.w, Event.resize.h); break; case SDL_VIDEOEXPOSE: render(); break; case SDL_MOUSEBUTTONDOWN: handle_mousedown(Event.button); break; case SDL_MOUSEBUTTONUP: handle_mouseup(Event.button); break; case SDL_MOUSEMOTION: handle_mousemove(Event.motion); break; case SDL_ACTIVEEVENT: // lose or gain focus break; case SDL_KEYDOWN: handle_keydown(Event.key); break; case SDL_KEYUP: handle_keyup(Event.key); break; case SDL_USEREVENT: handle_userevent(Event.user); break; default: printf("SDL Event %d\n", Event.type); break; } //idle code //if (busy) // timerIdle = SDL_AddTimer(20, &timerCallback, (void *) TIMER_IDLE); } if (timerKeyRepeat) SDL_RemoveTimer(timerKeyRepeat); if (timerDragRender) SDL_RemoveTimer(timerDragRender); free(layer); SDL_FreeSurface(Surf_Display); SDL_Quit(); return 0; }
void drawPhong(int dFlag) { poly *p, *head; polyList *s; int i,j,hue; int *anIndex, redo; viewTriple *aPoint, *polyPt; redo = (recalc || redoSmooth); if (redo || redoColor || redoDither) { rotated = no; zoomed = no; translated = no; switchedPerspective = no; changedEyeDistance = no; redoSmooth = no; movingLight = no; /* If only a color change don't recalculate polygon info. */ if (!redo) { /* glossy shading if a single hue is indicated */ changeColorMap(); scanLines(dFlag); /* if axes are on then show axes labels */ if (viewport->axesOn) showAxesLabels(dFlag); /* show pixmap of image */ XCopyArea(dsply,viewmap,viewport->viewWindow,trashGC,0,0, vwInfo.width,vwInfo.height,0,0); } else { if (keepDrawingViewport()) { if (!firstTime && !(scanline > 0)) { strcpy(control->message," Freeing Polygons "); writeControlMessage(); freeListOfPolygons(quickList); freePointResevoir(); } if (keepDrawingViewport()) { strcpy(control->message," Collecting Polygons "); writeControlMessage(); quickList = copyPolygons(viewData.polygons); if (keepDrawingViewport()) { strcpy(control->message," Projecting Polygons "); writeControlMessage(); projectAllPolys(quickList); if (keepDrawingViewport()) { strcpy(control->message, " Setting Polygon Extremes "); writeControlMessage(); minMaxPolygons(quickList); if (keepDrawingViewport()) { strcpy(control->message, " Sorting Polygons "); writeControlMessage(); quickList = msort(quickList,0,viewData.numPolygons, polyCompare); calcEyePoint(); head = p = quickList; /* glossy shading if a single hue is indicated */ changeColorMap(); for (i=0, aPoint=viewData.points; i<viewData.numOfPoints; i++,aPoint++) { aPoint->norm[0]= 0.0; aPoint->norm[1]= 0.0; aPoint->norm[2]= 0.0; } freePolyList(); for (i = 0; i < ARRAY_HEIGHT; i++) scanList[i] = NIL(polyList); /* for each polygon */ /* calculate average normal for each vertex */ strcpy(control->message, " Build Polygon Lists "); writeControlMessage(); p = head; while ((p != NIL(poly)) && keepDrawingViewport()) { for (j = 0, anIndex = p->indexPtr; j < p->numpts; j++, anIndex++) { polyPt = refPt3D(viewData,*(anIndex)); polyPt->norm[0] += p->N[0]; polyPt->norm[1] += p->N[1]; polyPt->norm[2] += p->N[2]; normalizeVector(polyPt->norm); /* get hue for each point if multi-dithering is used */ if ((viewport->hueOffset != viewport->hueTop || smoothError) && !mono) { if (absolute(p->color) > 1.0) { hue = floor(absolute(p->color)); } else { hue = floor(absolute(p->color) * viewport->numberOfHues) + viewport->hueOffset; } polyPt->sc = (float)hue; } /* multi-color dither */ } /* for each point in polygon */ if ( ! ( p->partialClipPz || p->totalClipPz || (viewData.clipStuff && (p->partialClip || p->totalClip ) ) ) ) { /* put polygon in each scanline list it intersects */ for (i=(int)p->pymin; i<= (int)p->pymax; i++) { if ( (i>=0) && (i<ARRAY_HEIGHT ) ){ s = (polyList *)saymem("smoothShade.c",1,sizeof(polyList)); s->polyIndx = p; s->next = scanList[i]; scanList[i] = s; } } /* put polygon in each scanline it intersects */ } /* if polygon not clipped */ p = p->next; } /* while still polygons */ scanLines(dFlag); /* if axes are on then show axes labels */ if (viewport->axesOn) showAxesLabels(dFlag); /* show pixmap of image */ XCopyArea(dsply,viewmap,viewport->viewWindow,trashGC,0,0, vwInfo.width,vwInfo.height,0,0); /* freePolyList(scanList); */ } /* keepDrawingViewport() after setting extreme values */ } /* keepDrawingViewport() after projecting all polygons */ } /* keepDrawingViewport() after collecting polygons */ } /* keepDrawingViewport() after freeing polygons */ } /* keepDrawingViewport() after recalc */ finishedList = !(scanline>0); if (firstTime) firstTime = no; } /* not only a color change */ } else { /* else just redisplay current pixmap of image */ XCopyArea(dsply,viewmap,viewport->viewWindow,trashGC,0,0, vwInfo.width,vwInfo.height,0,0); } clearControlMessage(); strcpy(control->message,viewport->title); writeControlMessage(); } /* drawPhong */
bool GroundPlaneGenerator::getBoardRects(const QString & boardSvg, QGraphicsItem * board, double res, double keepoutSpace, QList<QRect> & rects) { QByteArray boardByteArray; QString tempColor("#000000"); QStringList exceptions; exceptions << "none" << ""; if (!SvgFileSplitter::changeColors(boardSvg, tempColor, exceptions, boardByteArray)) { return false; } QRectF br = board->sceneBoundingRect(); double bWidth = res * br.width() / FSvgRenderer::printerScale(); double bHeight = res * br.height() / FSvgRenderer::printerScale(); QImage image(bWidth, bHeight, QImage::Format_Mono); // Format_RGB32 image.setDotsPerMeterX(res * GraphicsUtils::InchesPerMeter); image.setDotsPerMeterY(res * GraphicsUtils::InchesPerMeter); image.fill(0xffffffff); QSvgRenderer renderer(boardByteArray); QPainter painter; painter.begin(&image); painter.setRenderHint(QPainter::Antialiasing, false); renderer.render(&painter); painter.end(); #ifndef QT_NO_DEBUG //image.save("getBoardRects.png"); #endif QColor keepaway(255,255,255); int threshold = 1; // now add keepout area to the border QImage image2 = image.copy(); painter.begin(&image2); painter.setRenderHint(QPainter::Antialiasing, false); painter.fillRect(0, 0, image2.width(), keepoutSpace, keepaway); painter.fillRect(0, image2.height() - keepoutSpace, image2.width(), keepoutSpace, keepaway); painter.fillRect(0, 0, keepoutSpace, image2.height(), keepaway); painter.fillRect(image2.width() - keepoutSpace, 0, keepoutSpace, image2.height(), keepaway); for (int y = 0; y < image.height(); y++) { for (int x = 0; x < image.width(); x++) { QRgb current = image.pixel(x, y); int gray = QGRAY(current); if (gray <= threshold) { continue; } painter.fillRect(x - keepoutSpace, y - keepoutSpace, keepoutSpace + keepoutSpace, keepoutSpace + keepoutSpace, keepaway); } } painter.end(); #ifndef QT_NO_DEBUG //image2.save("getBoardRects2.png"); #endif scanLines(image2, bWidth, bHeight, rects, threshold, 1); // combine parallel equal-sized rects int ix = 0; while (ix < rects.count()) { QRect r = rects.at(ix++); for (int j = ix; j < rects.count(); j++) { QRect s = rects.at(j); if (s.bottom() == r.bottom()) { // on same row; keep going continue; } if (s.top() > r.bottom() + 1) { // skipped row, can't join break; } if (s.left() == r.left() && s.right() == r.right()) { // join these r.setBottom(s.bottom()); rects.removeAt(j); ix--; rects.replace(ix, r); break; } } } return true; }