std::list<KDPoint> randomPointsInBox(int numPoints, KDPoint min, KDPoint max) { CGAL::Random_points_in_cube_3<KDPoint, Pt_creator> cubeRnd(1.0); std::list<KDPoint> randomPointsCube; CGAL::copy_n(cubeRnd, numPoints, std::back_inserter(randomPointsCube)); std::list<KDPoint> randomPoints; std::list<KDPoint>::iterator pointsIter; for(pointsIter = randomPointsCube.begin(); pointsIter != randomPointsCube.end(); ++pointsIter) { KDPoint point = *pointsIter; KDTransformation translate(CGAL::TRANSLATION, KDVector(1.0, 1.0, 1.0)); KDTransformation scale(CGAL::SCALING, 0.5); point = translate(point); point = scale(point); KDTransformation scaleToBox((max.x()-min.x()), 0, 0, 0, (max.y()-min.y()), 0, 0, 0, (max.z()-min.z())); KDTransformation translateToBox(CGAL::TRANSLATION, KDVector(min.x(), min.y(), min.z())); point = scaleToBox(point); point = translateToBox(point); randomPoints.push_back(point); } return randomPoints; }
void BracketPairLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) { const KDCoordinate k_widthMargin = widthMargin(); const KDCoordinate k_externWidthMargin = externWidthMargin(); KDSize operandSize = operandLayout()->size(); KDCoordinate verticalBarHeight = operandLayout()->size().height() + 2*k_verticalMargin; ctx->fillRect(KDRect(p.x()+k_externWidthMargin, p.y()+verticalExternMargin(), k_lineThickness, verticalBarHeight), expressionColor); ctx->fillRect(KDRect(p.x()+k_externWidthMargin+operandSize.width()+2*k_widthMargin+k_lineThickness, p.y()+verticalExternMargin(), k_lineThickness, verticalBarHeight), expressionColor); if (renderTopBar()) { ctx->fillRect(KDRect(p.x()+k_externWidthMargin, p.y()+verticalExternMargin(), k_bracketWidth, k_lineThickness), expressionColor); ctx->fillRect(KDRect(p.x()+k_externWidthMargin+2*k_lineThickness+operandSize.width()+2*k_widthMargin-k_bracketWidth, p.y() + verticalExternMargin(), k_bracketWidth, k_lineThickness), expressionColor); } if (renderBottomBar()) { ctx->fillRect(KDRect(p.x()+k_externWidthMargin, p.y()+verticalExternMargin()+verticalBarHeight-k_lineThickness, k_bracketWidth, k_lineThickness), expressionColor); ctx->fillRect(KDRect(p.x()+k_externWidthMargin+2*k_lineThickness+operandSize.width()+2*k_widthMargin-k_bracketWidth, p.y()+verticalExternMargin()+verticalBarHeight-k_lineThickness, k_bracketWidth, k_lineThickness), expressionColor); } }
void KDContext::drawLine(KDPoint p1, KDPoint p2, KDColor c) { // Find the largest gap KDPoint left = KDPointZero, right = KDPointZero; if (p2.x() > p1.x()) { left = p1; right = p2; } else { left = p2; right = p1; } KDPoint top = KDPointZero, bottom = KDPointZero; if (p2.y() > p1.y()) { top = p1; bottom = p2; } else { top = p2; bottom = p1; } assert(right.x() >= left.x()); assert(bottom.y() >= top.y()); KDCoordinate deltaX = 2*(right.x() - left.x()); KDCoordinate deltaY = 2*(bottom.y() - top.y()); KDPoint p = KDPointZero, alwaysTranslate = KDPointZero, conditionalTranslate = KDPointZero; KDCoordinate scanLength, error, minusError, plusError; if (deltaX >= deltaY) { p = left; scanLength = right.x() - left.x(); error = right.x() - left.x(); minusError = deltaY; plusError = deltaX; alwaysTranslate = KDPoint(1,0); conditionalTranslate = KDPoint(0, (right.y() >= left.y() ? 1 : -1)); } else { p = top; scanLength = bottom.y() - top.y(); error = bottom.y() - top.y(); minusError = deltaX; plusError = deltaY; alwaysTranslate = KDPoint(0,1); conditionalTranslate = KDPoint((bottom.x() >= top.x() ? 1 : -1), 0); } KDCoordinate scanCounter = 0; while (scanCounter++ < scanLength) { setPixel(p, c); p = p.translatedBy(alwaysTranslate); error = error - minusError; if (error <= 0) { p = p.translatedBy(conditionalTranslate); error = error + plusError; } } }
void LeftParenthesisLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) { KDRect frame(p.x()+ParenthesisLayout::k_externWidthMargin, p.y()+ParenthesisLayout::k_externHeightMargin, ParenthesisLayout::k_parenthesisCurveWidth, ParenthesisLayout::k_parenthesisCurveHeight); ctx->blendRectWithMask(frame, expressionColor, (const uint8_t *)topLeftCurve, (KDColor *)(ParenthesisLayout::s_parenthesisWorkingBuffer)); frame = KDRect(p.x()+ParenthesisLayout::k_externWidthMargin, p.y() + size().height() - ParenthesisLayout::k_parenthesisCurveHeight - ParenthesisLayout::k_externHeightMargin, ParenthesisLayout::k_parenthesisCurveWidth, ParenthesisLayout::k_parenthesisCurveHeight); ctx->blendRectWithMask(frame, expressionColor, (const uint8_t *)bottomLeftCurve, (KDColor *)(ParenthesisLayout::s_parenthesisWorkingBuffer)); ctx->fillRect(KDRect(p.x()+ParenthesisLayout::k_externWidthMargin, p.y()+ParenthesisLayout::k_parenthesisCurveHeight+ParenthesisLayout::k_externHeightMargin, ParenthesisLayout::k_lineThickness, size().height() - 2*(ParenthesisLayout::k_parenthesisCurveHeight+ParenthesisLayout::k_externHeightMargin)), expressionColor); }
std::list<KDPoint> translatePoints(std::list<KDPoint> &listPoints, KDPoint vectorPoint) { std::list<KDPoint> translatedPoints; KDTransformation translate(CGAL::TRANSLATION, KDVector(vectorPoint.x(), vectorPoint.y(), vectorPoint.z())); std::list<KDPoint>::iterator vectorPointInter; for(vectorPointInter = listPoints.begin(); vectorPointInter != listPoints.end(); ++vectorPointInter) { KDPoint point = *vectorPointInter; translatedPoints.push_back(translate(point)); } return translatedPoints; }
std::list<KDPoint> randomPointsInConcaveSphere(int numPoints, double innerRadius, double outerRadius) { CGAL::Random_points_in_sphere_3<KDPoint, Pt_creator> sphereRnd(outerRadius - innerRadius); std::list<KDPoint> innerRandomPoints; CGAL::copy_n(sphereRnd, numPoints, std::back_inserter(innerRandomPoints)); std::list<KDPoint> randomPoints; std::list<KDPoint>::iterator pointsIter; for(pointsIter = innerRandomPoints.begin(); pointsIter != innerRandomPoints.end(); ++pointsIter) { KDPoint point = *pointsIter; KDVector vPoint1 = KDVector(point.x(), point.y(), point.z()); KDVector vPoint1Unit = vPoint1 / CGAL::sqrt(vPoint1.squared_length()); KDVector vPoint2 = vPoint1Unit * innerRadius; KDVector vPoint = vPoint1 + vPoint2; randomPoints.push_back(KDPoint(vPoint.x(), vPoint.y(), vPoint.z())); } return randomPoints; }
std::list<KDPoint> randomPointsInSphereGaussianDistribution(int numPoints, double radius, double width) { CGAL::Random_points_in_sphere_3<KDPoint, Pt_creator> sphereRnd(1.0); std::list<KDPoint> innerRandomPoints; CGAL::copy_n(sphereRnd, numPoints, std::back_inserter(innerRandomPoints)); std::list<KDPoint> randomPoints; std::list<KDPoint>::iterator pointsIter; for(pointsIter = innerRandomPoints.begin(); pointsIter != innerRandomPoints.end(); ++pointsIter) { KDPoint point = *pointsIter; KDVector vPointR = KDVector(point.x(), point.y(), point.z()); double r = CGAL::sqrt(vPointR.squared_length()); KDVector unitVector = vPointR / r; double gaussRadius = std::exp(-(width*r*r)); KDTransformation scaleGauss(CGAL::SCALING, gaussRadius); KDTransformation scaleRadius(CGAL::SCALING, radius); KDPoint scaledPoint = scaleGauss(KDPoint(unitVector.x(), unitVector.y(), unitVector.z())); scaledPoint = scaleRadius(scaledPoint); randomPoints.push_back(scaledPoint); } return randomPoints; }
KDPoint KDPoint::translatedBy(KDPoint other) const { return KDPoint(m_x+other.x(), m_y+other.y()); }
uint16_t KDPoint::squareDistanceTo(KDPoint other) const { return (m_x-other.x()) * (m_x-other.x()) + (m_y-other.y()) * (m_y-other.y()); }
//Call the rejointMesh in RejointMeshes.cpp on the meshes that have distance with targetPoint greater than double distance // use isExternJoin = true to joint the extern meshes, use isExternJoin = false to joint the internal meshes std::list<MeshData> rejointMeshesInDistance(std::list<MeshData> &listMeshData, KDPoint targetPoint, double distance, double isExternJoin) { std::list<MeshData> result; std::list<MeshData> meshesToJoin; double squaredDistance = distance * distance; PointCGAL exactTargetPoint(targetPoint.x(), targetPoint.y(), targetPoint.z()); std::list<MeshData>::iterator meshDataInter; for(meshDataInter = listMeshData.begin(); meshDataInter != listMeshData.end(); ++meshDataInter) { bool meshIsOutOfDistance = true; //Algorithm 1 //bool meshIsOutOfDistance = false; //Algorithm 2 std::list<Triangle>::iterator triangleIter; for(triangleIter = meshDataInter->first.begin(); triangleIter != meshDataInter->first.end(); ++triangleIter) { Triangle t = *triangleIter; //Algorithm 1: This algorithm excludes from the join the meshes on the radius for (int i = 0; i < 3; ++i) { PointCGAL tv = t.vertex(i); //it could be used a logic XNOR but it is not so understable, so I use this condition if (isExternJoin) { if (CGAL::squared_distance(tv, exactTargetPoint) < squaredDistance) { meshIsOutOfDistance = false; } } else { if (CGAL::squared_distance(tv, exactTargetPoint) > squaredDistance) { meshIsOutOfDistance = false; } } } if (!meshIsOutOfDistance) { break; } /* //Algorithm 2: This algorithm includes from the join the meshes on the radius //Ideale per pezzi piccoli e dominio dei punti voronoi dentro il raggio, // sembra funzionare solo per isExternJoin, TODO: test for (int i = 0; i < 3; ++i) { PointCGAL tv = t.vertex(i); if (isExternJoin) { if (CGAL::squared_distance(tv, exactTargetPoint) > squaredDistance) { meshIsOutOfDistance = true; } } else { if (CGAL::squared_distance(tv, exactTargetPoint) < squaredDistance) { meshIsOutOfDistance = true; } } } if (meshIsOutOfDistance) { break; } */ } if (meshIsOutOfDistance) { meshesToJoin.push_back(*meshDataInter); } else { result.push_back(*meshDataInter); } } if (meshesToJoin.size() > 0) { MeshData rejointMeshes = simpleRejointMeshes(meshesToJoin); //MeshData rejointMeshes = rejointMeshes(meshesToJoin); result.push_back(rejointMeshes); } std::cout << "Executed rejointMeshesInDistance: not joined meshes: " << (listMeshData.size() - meshesToJoin.size()) << ", joined meshes: " << meshesToJoin.size() << std::endl; return result; }
PointCGAL converPointInexactToExact(KDPoint point) { return PointCGAL(CGAL::to_double(point.x()), CGAL::to_double(point.y()), CGAL::to_double(point.z())); }
void RightSquareBracketLayout::render(KDContext * ctx, KDPoint p, KDColor expressionColor, KDColor backgroundColor) { ctx->fillRect(KDRect(p.x()+k_widthMargin, p.y(), k_lineThickness, operandHeight()), expressionColor); ctx->fillRect(KDRect(p.x()+k_widthMargin-k_bracketWidth+1, p.y(), k_bracketWidth, k_lineThickness), expressionColor); ctx->fillRect(KDRect(p.x()+k_widthMargin-k_bracketWidth+1, p.y() + operandHeight(), k_bracketWidth, k_lineThickness), expressionColor); }