Below is sample code on how to generate points on a circle from CircleKit (center_pos, radius, vector_normal).
#include <iostream>
#include <vector>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
struct Vector3 {
double x, y, z;
Vector3() : x(0), y(0), z(0) {}
Vector3(double x, double y, double z) : x(x), y(y), z(z) {}
Vector3 operator+(const Vector3& v) const {
return Vector3(x + v.x, y + v.y, z + v.z);
}
Vector3 operator*(double scalar) const {
return Vector3(x * scalar, y * scalar, z * scalar);
}
Vector3 cross(const Vector3& v) const {
return Vector3(
y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x
);
}
Vector3 normalize() const {
double length = std::sqrt(x * x + y * y + z * z);
return Vector3(x / length, y / length, z / length);
}
};
std::vector<Vector3> computeCirclePoints3D(const Vector3& center, double radius, const Vector3& normal, int numPoints) {
std::vector<Vector3> points;
points.reserve(numPoints);
// Generate a basis for the plane
Vector3 arbitraryVec(1, 0, 0); // Choose an arbitrary vector
if (std::abs(normal.x) < 1e-6 && std::abs(normal.y) < 1e-6) {
arbitraryVec = Vector3(0, 1, 0); // Handle edge case when normal is along Z-axis
}
Vector3 U = normal.cross(arbitraryVec).normalize();
Vector3 V = normal.cross(U).normalize();
// Angle between each point
double angleIncrement = 2.0 * M_PI / numPoints;
for (int i = 0; i < numPoints; ++i) {
double angle = i * angleIncrement;
Vector3 point = center + (U * (radius * cos(angle))) + (V * (radius * sin(angle)));
points.push_back(point);
}
return points;
}
void CHPSView::OnUserCode4()
{
Vector3 center(0, 0, 0);
double radius = 10.0;
Vector3 normal(0, 0, 1);
// amount of points to generate
int numPoints = 8;
HPS::SegmentKey modelKey = GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey();
modelKey.Subsegment("circle").InsertCircle(HPS::Point(0, 0, 0), (float)radius, HPS::Vector(0, 0, 1));
std::vector<Vector3> points = computeCirclePoints3D(center, radius, normal, numPoints);
modelKey.Subsegment("markers").GetVisibilityControl().SetMarkers(true);
modelKey.Subsegment("markers").GetMaterialMappingControl().SetMarkerColor(HPS::RGBAColor(1, 0, 0));
for (const auto& point : points) {
modelKey.Subsegment("markers").InsertMarker(HPS::Point(point.x, point.y, point.z));
}
GetCanvas().GetFrontView().FitWorld().UpdateWithNotifier().Wait();
//return 0;
}
//! [user_code]