void addLineInfill(Polygons& result, PointMatrix matrix, int scanline_min_idx, int lineSpacing, AABB boundary, std::vector<std::vector<int64_t> > cutList, int extrusionWidth) { auto addLine = [&](Point from, Point to) { PolygonRef p = result.newPoly(); p.add(matrix.unapply(from)); p.add(matrix.unapply(to)); }; auto compare_int64_t = [](const void* a, const void* b) { int64_t n = (*(int64_t*)a) - (*(int64_t*)b); if (n < 0) return -1; if (n > 0) return 1; return 0; }; int scanline_idx = 0; for(int64_t x = scanline_min_idx * lineSpacing; x < boundary.max.X; x += lineSpacing) { qsort(cutList[scanline_idx].data(), cutList[scanline_idx].size(), sizeof(int64_t), compare_int64_t); for(unsigned int i = 0; i + 1 < cutList[scanline_idx].size(); i+=2) { if (cutList[scanline_idx][i+1] - cutList[scanline_idx][i] < extrusionWidth / 5) continue; addLine(Point(x, cutList[scanline_idx][i]), Point(x, cutList[scanline_idx][i+1])); } scanline_idx += 1; } }
void drawSquares(IplImage* img, CvSeq* squares, PointMatrix &mat) { CvSeqReader reader; IplImage* cpy = cvCloneImage(img); cvStartReadSeq(squares, &reader, 0); for (int i = 0; i < squares->total; i++) { CvPoint pt[4], *rect = pt; int count = 4; CV_READ_SEQ_ELEM(pt[0], reader); CV_READ_SEQ_ELEM(pt[1], reader); CV_READ_SEQ_ELEM(pt[2], reader); CV_READ_SEQ_ELEM(pt[3], reader); cvLine(cpy, pt[0], pt[2], CV_RGB(0, 0, 0), 1); cvLine(cpy, pt[1], pt[3], CV_RGB(255, 255, 0), 1); MyCvPoint myCvPoint( (pt[0].x + pt[2].x) /2, (pt[1].y + pt[2].y)/2, img->width, img->height); mat.AddMem(myCvPoint); cvPolyLine(cpy, &rect, &count, 1, 1, CV_RGB(0, 255, 255), 1, 8, 0); } // cvShowImage("After Modify", cpy); cvReleaseImage(&cpy); }
void Infill::addLineInfill(Polygons& result, const PointMatrix& rotation_matrix, const int scanline_min_idx, const int line_distance, const AABB boundary, std::vector<std::vector<int64_t>>& cut_list, int64_t shift) { auto compare_int64_t = [](const void* a, const void* b) { int64_t n = (*(int64_t*)a) - (*(int64_t*)b); if (n < 0) { return -1; } if (n > 0) { return 1; } return 0; }; int scanline_idx = 0; for(int64_t x = scanline_min_idx * line_distance + shift; x < boundary.max.X; x += line_distance) { std::vector<int64_t>& crossings = cut_list[scanline_idx]; qsort(crossings.data(), crossings.size(), sizeof(int64_t), compare_int64_t); for(unsigned int crossing_idx = 0; crossing_idx + 1 < crossings.size(); crossing_idx += 2) { if (crossings[crossing_idx + 1] - crossings[crossing_idx] < infill_line_width / 5) { // segment is too short to create infill continue; } result.addLine(rotation_matrix.unapply(Point(x, crossings[crossing_idx])), rotation_matrix.unapply(Point(x, crossings[crossing_idx + 1]))); } scanline_idx += 1; } }
bool polygonCollidesWithlineSegment(PolygonRef poly, Point& transformed_startPoint, Point& transformed_endPoint, PointMatrix transformation_matrix) { Point p0 = transformation_matrix.apply(poly.back()); for(Point p1_ : poly) { Point p1 = transformation_matrix.apply(p1_); if ((p0.Y > transformed_startPoint.Y && p1.Y < transformed_startPoint.Y) || (p1.Y > transformed_startPoint.Y && p0.Y < transformed_startPoint.Y)) { int64_t x = p0.X + (p1.X - p0.X) * (transformed_startPoint.Y - p0.Y) / (p1.Y - p0.Y); if (x > transformed_startPoint.X && x < transformed_endPoint.X) return true; } p0 = p1; } return false; }
int CBitMask_Test() { { CBitMask< int > test_map; // operator overload test_map[ bitmask::Point( 0, 0 ) ] = 1; test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 1 ); // at test_map.At( CBitMask< int >::Pos( 1, 1 ) ) = 2; test_assert( test_map.At( CBitMask< int >::Pos( 1, 1 ) ) == 2 ); } // assign & clear tests { CBitMask< int > test_map1; test_assert( test_map1.Empty() ); test_map1[ bitmask::Point( 0, 0 ) ] = 1; test_assert( test_map1.Empty() == false ); test_map1.Clear(); test_assert( test_map1.Empty() == true ); CBitMask< int > test_map2; test_map2[ bitmask::Point( 5, 5 ) ] = 5; test_assert( test_map1.Empty() == true ); test_map1 = test_map2; test_assert( test_map1.Empty() == false ); test_assert( test_map2[ bitmask::Point( 5, 5 ) ] == 5 ); test_assert( test_map1[ bitmask::Point( 5, 5 ) ] == 5 ); CBitMask< int > test_map3( test_map1 ); test_assert( test_map3.Empty() == false ); test_assert( test_map3[ bitmask::Point( 5, 5 ) ] == test_map1[ bitmask::Point( 5, 5 ) ] ); } // testing iterators { CBitMask< int > test_map; int object_count = 0; for( CBitMask< int >::Iterator i = test_map.Begin(); i != test_map.End(); ++i ) { object_count++; } test_assert( object_count == 0 ); test_map[ bitmask::Point( 3, 4 ) ] = 5; object_count = 0; for( CBitMask< int >::Iterator i = test_map.Begin(); i != test_map.End(); ++i ) { object_count++; test_assert( i->first == bitmask::Point( 3, 4 ) ); test_assert( i->second == 5 ); } test_assert( object_count == 1 ); // clear things and test that iterator count == 0 test_map.Clear(); object_count = 0; for( CBitMask< int >::Iterator i = test_map.Begin(); i != test_map.End(); ++i ) { object_count++; } test_assert( object_count == 0 ); } // testing assigning things // test Get3x3CBitMask :) { CBitMask< int > test_map; // 1 2 3 // 4 5 6 // 7 8 9 test_map = Get3x3CBitMask(); test_assert( test_map[ bitmask::Point( -1, -1 ) ] == 1 ); test_assert( test_map[ bitmask::Point( 0, -1 ) ] == 2 ); test_assert( test_map[ bitmask::Point( 1, -1 ) ] == 3 ); test_assert( test_map[ bitmask::Point( -1, 0 ) ] == 4 ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 5 ); test_assert( test_map[ bitmask::Point( 1, 0 ) ] == 6 ); test_assert( test_map[ bitmask::Point( -1, 1 ) ] == 7 ); test_assert( test_map[ bitmask::Point( 0, 1 ) ] == 8 ); test_assert( test_map[ bitmask::Point( 1, 1 ) ] == 9 ); } // multiply test { CBitMask< int > test_map; // operator overload test_map[ bitmask::Point( 0, 0 ) ] = 1; test_map[ bitmask::Point( 3, 1 ) ] = 2; bitmask::PointMatrix mat; mat.Set( 3.1415962f ); test_map.Multiply( mat ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 1 ); test_assert( test_map[ bitmask::Point( -3, -1 ) ] == 2 ); test_assert( test_map[ bitmask::Point( 3, 1 ) ] == 0 ); } // test multiply stuff { using namespace bitmask; CBitMask< int > test_map; test_map = Get3x3CBitMask(); // 1 2 3 // 4 5 6 // 7 8 9 // LET'S F*****G TRANSFORM IT! PointMatrix mat; mat.Set( 3.1415962f ); // -1, 0 // 0, -1 test_assert( mat.col1.x == -1 ); test_assert( mat.col2.x == 0 ); test_assert( mat.col1.y == 0 ); test_assert( mat.col2.y == -1 ); // before multiply test_assert( test_map[ bitmask::Point( -1, -1 ) ] == 1 ); test_assert( test_map[ bitmask::Point( 0, -1 ) ] == 2 ); test_assert( test_map[ bitmask::Point( 1, -1 ) ] == 3 ); test_assert( test_map[ bitmask::Point( -1, 0 ) ] == 4 ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 5 ); test_assert( test_map[ bitmask::Point( 1, 0 ) ] == 6 ); test_assert( test_map[ bitmask::Point( -1, 1 ) ] == 7 ); test_assert( test_map[ bitmask::Point( 0, 1 ) ] == 8 ); test_assert( test_map[ bitmask::Point( 1, 1 ) ] == 9 ); test_map.Multiply( mat ); // 1 2 3 9 8 7 // 4 5 6 -> 6 5 4 // 7 8 9 3 2 1 test_assert( test_map[ bitmask::Point( -1, -1 ) ] == 9 ); test_assert( test_map[ bitmask::Point( 0, -1 ) ] == 8 ); test_assert( test_map[ bitmask::Point( 1, -1 ) ] == 7 ); test_assert( test_map[ bitmask::Point( -1, 0 ) ] == 6 ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 5 ); test_assert( test_map[ bitmask::Point( 1, 0 ) ] == 4 ); test_assert( test_map[ bitmask::Point( -1, 1 ) ] == 3 ); test_assert( test_map[ bitmask::Point( 0, 1 ) ] == 2 ); test_assert( test_map[ bitmask::Point( 1, 1 ) ] == 1 ); } // rotate left { using namespace bitmask; CBitMask< int > test_map; test_map = Get3x3CBitMask(); // 1 2 3 // 4 5 6 // 7 8 9 // LET'S F*****G TRANSFORM IT! PointMatrix mat; mat.Set( 3.1415962f * 0.5f ); // 0, -1 // 1, 0 test_assert( mat.col1.x == 0 ); test_assert( mat.col2.x == -1 ); test_assert( mat.col1.y == 1 ); test_assert( mat.col2.y == 0 ); // before multiply test_assert( test_map[ bitmask::Point( -1, -1 ) ] == 1 ); test_assert( test_map[ bitmask::Point( 0, -1 ) ] == 2 ); test_assert( test_map[ bitmask::Point( 1, -1 ) ] == 3 ); test_assert( test_map[ bitmask::Point( -1, 0 ) ] == 4 ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 5 ); test_assert( test_map[ bitmask::Point( 1, 0 ) ] == 6 ); test_assert( test_map[ bitmask::Point( -1, 1 ) ] == 7 ); test_assert( test_map[ bitmask::Point( 0, 1 ) ] == 8 ); test_assert( test_map[ bitmask::Point( 1, 1 ) ] == 9 ); test_map.Multiply( mat ); // const Point point( 1, 3 ); // test_assert( result == Point( -3, 1 ) ); // 1 2 3 7 4 1 // 4 5 6 -> 8 5 2 // 7 8 9 9 6 3 test_assert( test_map[ bitmask::Point( -1, -1 ) ] == 7 ); test_assert( test_map[ bitmask::Point( 0, -1 ) ] == 4 ); test_assert( test_map[ bitmask::Point( 1, -1 ) ] == 1 ); test_assert( test_map[ bitmask::Point( -1, 0 ) ] == 8 ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 5 ); test_assert( test_map[ bitmask::Point( 1, 0 ) ] == 2 ); test_assert( test_map[ bitmask::Point( -1, 1 ) ] == 9 ); test_assert( test_map[ bitmask::Point( 0, 1 ) ] == 6 ); test_assert( test_map[ bitmask::Point( 1, 1 ) ] == 3 ); } // rotate left { using namespace bitmask; CBitMask< int > test_map; test_map = Get3x3CBitMask(); // 1 2 3 // 4 5 6 // 7 8 9 // LET'S F*****G TRANSFORM IT! PointMatrix mat; mat.Set( 3.1415962f + 3.1415962f * 0.5f ); // 0, 1 // -1, 0 test_assert( mat.col1.x == 0 ); test_assert( mat.col2.x == 1 ); test_assert( mat.col1.y == -1 ); test_assert( mat.col2.y == 0 ); // before multiply test_assert( test_map[ bitmask::Point( -1, -1 ) ] == 1 ); test_assert( test_map[ bitmask::Point( 0, -1 ) ] == 2 ); test_assert( test_map[ bitmask::Point( 1, -1 ) ] == 3 ); test_assert( test_map[ bitmask::Point( -1, 0 ) ] == 4 ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 5 ); test_assert( test_map[ bitmask::Point( 1, 0 ) ] == 6 ); test_assert( test_map[ bitmask::Point( -1, 1 ) ] == 7 ); test_assert( test_map[ bitmask::Point( 0, 1 ) ] == 8 ); test_assert( test_map[ bitmask::Point( 1, 1 ) ] == 9 ); test_map.Multiply( mat ); // const Point point( 1, 3 ); // test_assert( result == Point( 3, -1 ) ); // 1 2 3 3 6 9 // 4 5 6 -> 2 5 8 // 7 8 9 1 4 7 test_assert( test_map[ bitmask::Point( -1, -1 ) ] == 3 ); test_assert( test_map[ bitmask::Point( 0, -1 ) ] == 6 ); test_assert( test_map[ bitmask::Point( 1, -1 ) ] == 9 ); test_assert( test_map[ bitmask::Point( -1, 0 ) ] == 2 ); test_assert( test_map[ bitmask::Point( 0, 0 ) ] == 5 ); test_assert( test_map[ bitmask::Point( 1, 0 ) ] == 8 ); test_assert( test_map[ bitmask::Point( -1, 1 ) ] == 1 ); test_assert( test_map[ bitmask::Point( 0, 1 ) ] == 4 ); test_assert( test_map[ bitmask::Point( 1, 1 ) ] == 7 ); } return 0; }