Skip to content

Commit 6a9f82c

Browse files
committed
More work on point
1 parent e826394 commit 6a9f82c

File tree

2 files changed

+148
-62
lines changed

2 files changed

+148
-62
lines changed

modules/yup_graphics/primitives/yup_Point.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,14 @@ class YUP_API Point
315315
316316
@return A new point on the specified circle.
317317
*/
318-
[[nodiscard]] constexpr Point getPointOnCircumference (float radius, float angleRadians) const noexcept
318+
template <class T = ValueType>
319+
[[nodiscard]] constexpr auto getPointOnCircumference (float radius, float angleRadians) const noexcept
320+
-> std::enable_if_t<std::is_floating_point_v<T>, Point>
319321
{
320-
return { x + (std::cos (angleRadians) * radius), y + std::sin (angleRadians) * radius };
322+
return {
323+
static_cast<ValueType> (x + (std::cos (angleRadians) * radius)),
324+
static_cast<ValueType> (y + std::sin (angleRadians) * radius)
325+
};
321326
}
322327

323328
/** Returns a new point located on the circumference of an ellipse centered at this point, given radii and an angle.
@@ -331,9 +336,14 @@ class YUP_API Point
331336
332337
@return A new point on the specified ellipse.
333338
*/
334-
[[nodiscard]] constexpr Point getPointOnCircumference (float radiusX, float radiusY, float angleRadians) const noexcept
339+
template <class T = ValueType>
340+
[[nodiscard]] constexpr auto getPointOnCircumference (float radiusX, float radiusY, float angleRadians) const noexcept
341+
-> std::enable_if_t<std::is_floating_point_v<T>, Point>
335342
{
336-
return { x + (std::cos (angleRadians) * radiusX), y + std::sin (angleRadians) * radiusY };
343+
return {
344+
static_cast<ValueType> (x + (std::cos (angleRadians) * radiusX)),
345+
static_cast<ValueType> (y + std::sin (angleRadians) * radiusY)
346+
};
337347
}
338348

339349
//==============================================================================

modules/yup_python/bindings/yup_YupGraphics_bindings.cpp

Lines changed: 134 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ using namespace py::literals;
4747
template <template <class> class Class, class... Types>
4848
void registerPoint (py::module_& m)
4949
{
50+
// clang-format off
51+
5052
py::dict type;
5153

5254
([&]
@@ -57,76 +59,150 @@ void registerPoint (py::module_& m)
5759
const auto className = Helpers::pythonizeCompoundClassName ("Point", typeid (Types).name());
5860

5961
auto class_ = py::class_<T> (m, className.toRawUTF8())
60-
.def (py::init<>())
61-
.def (py::init<ValueType, ValueType>())
62-
.def (py::self == py::self)
63-
.def (py::self != py::self)
64-
.def ("isOrigin", &T::isOrigin)
65-
//.def ("isFinite", &T::isFinite)
66-
.def ("getX", &T::getX)
67-
.def ("getY", &T::getY)
68-
.def ("setX", &T::setX)
69-
.def ("setY", &T::setY)
70-
.def ("withX", &T::withX)
71-
.def ("withY", &T::withY)
72-
//.def ("setXY", &T::setXY)
73-
//.def ("addXY", &T::addXY)
74-
//.def ("translated", &T::translated)
75-
.def (py::self + py::self)
76-
.def (py::self += py::self)
77-
.def (py::self - py::self)
78-
.def (py::self -= py::self)
79-
.def (py::self * py::self)
80-
.def (py::self *= py::self)
81-
.def (py::self * float())
82-
.def (py::self *= float())
83-
.def (py::self / py::self)
84-
.def (py::self /= py::self)
85-
.def (py::self / float())
86-
.def (py::self /= float())
87-
.def (-py::self)
88-
//.def ("getDistanceFromOrigin", &T::getDistanceFromOrigin)
89-
//.def ("getDistanceFrom", &T::getDistanceFrom)
90-
//.def ("getDistanceSquaredFromOrigin", &T::getDistanceSquaredFromOrigin)
91-
//.def ("getDistanceSquaredFrom", &T::getDistanceSquaredFrom)
92-
//.def ("getAngleToPoint", &T::getAngleToPoint)
93-
//.def ("getPointOnCircumference", py::overload_cast<float, float>(&T::getPointOnCircumference, py::const_))
94-
//.def ("getPointOnCircumference", py::overload_cast<float, float, float>(&T::getPointOnCircumference, py::const_))
95-
//.def ("getDotProduct", &T::getDotProduct)
96-
//.def ("applyTransform", &T::applyTransform)
97-
//.def ("transformedBy", &T::transformedBy)
98-
//.def ("toInt", &T::toInt)
99-
//.def ("toFloat", &T::toFloat)
100-
//.def ("toDouble", &T::toDouble)
101-
//.def ("roundToInt", &T::roundToInt)
102-
//.def ("toString", &T::toString)
103-
//.def_property("x", &T::getX, &T::setX)
104-
//.def_property("y", &T::getY, &T::setY)
105-
.def ("__repr__", [] (const T& self)
106-
{
107-
String result;
108-
result
109-
<< Helpers::pythonizeModuleClassName (PythonModuleName, typeid (self).name())
110-
<< "(" << self.getX() << ", " << self.getY() << ")";
111-
return result;
112-
})
113-
//.def ("__str__", &T::toString)
114-
;
62+
// Constructors
63+
.def (py::init<>())
64+
.def (py::init<ValueType, ValueType>())
65+
.def (py::init<const T&>())
66+
67+
// Basic methods
68+
.def ("isOrigin", &T::isOrigin)
69+
.def ("isOnXAxis", &T::isOnXAxis)
70+
.def ("isOnYAxis", &T::isOnYAxis)
71+
.def ("getX", &T::getX)
72+
.def ("getY", &T::getY)
73+
.def ("setX", &T::setX)
74+
.def ("setY", &T::setY)
75+
.def ("withX", &T::withX)
76+
.def ("withY", &T::withY)
77+
.def ("withXY", &T::withXY)
78+
79+
// Distance methods
80+
.def ("distanceTo", &T::distanceTo)
81+
.def ("distanceToSquared", &T::distanceToSquared)
82+
.def ("horizontalDistanceTo", &T::horizontalDistanceTo)
83+
.def ("verticalDistanceTo", &T::verticalDistanceTo)
84+
.def ("manhattanDistanceTo", &T::manhattanDistanceTo)
85+
86+
// Vector operations
87+
.def ("magnitude", &T::magnitude)
88+
.def ("dotProduct", &T::dotProduct)
89+
.def ("crossProduct", &T::crossProduct)
90+
.def ("angleTo", &T::angleTo)
91+
.def ("normalize", &T::normalize)
92+
.def ("normalized", &T::normalized)
93+
.def ("isNormalized", &T::isNormalized)
94+
95+
// Geometric operations
96+
.def ("translate", py::overload_cast<ValueType, ValueType>(&T::translate))
97+
.def ("translate", py::overload_cast<const T&>(&T::translate))
98+
.def ("translated", py::overload_cast<ValueType, ValueType>(&T::translated, py::const_))
99+
.def ("translated", py::overload_cast<const T&>(&T::translated, py::const_))
100+
.def ("rotateClockwise", &T::rotateClockwise)
101+
.def ("rotatedClockwise", &T::rotatedClockwise)
102+
.def ("rotateCounterClockwise", &T::rotateCounterClockwise)
103+
.def ("rotatedCounterClockwise", &T::rotatedCounterClockwise)
104+
105+
// Utility methods
106+
.def ("midpoint", &T::midpoint)
107+
.def ("pointBetween", &T::pointBetween)
108+
.def ("isCollinear", &T::isCollinear)
109+
.def ("isWithinCircle", &T::isWithinCircle)
110+
.def ("isWithinRectangle", &T::isWithinRectangle)
111+
112+
// Reflection methods
113+
.def ("reflectOverXAxis", &T::reflectOverXAxis)
114+
.def ("reflectedOverXAxis", &T::reflectedOverXAxis)
115+
.def ("reflectOverYAxis", &T::reflectOverYAxis)
116+
.def ("reflectedOverYAxis", &T::reflectedOverYAxis)
117+
.def ("reflectOverOrigin", &T::reflectOverOrigin)
118+
.def ("reflectedOverOrigin", &T::reflectedOverOrigin)
119+
120+
// Math operations
121+
.def ("min", &T::min)
122+
.def ("max", &T::max)
123+
.def ("abs", &T::abs)
124+
.def ("lerp", &T::lerp)
125+
126+
// Transformation
127+
.def ("transform", &T::transform)
128+
.def ("transformed", &T::transformed)
129+
130+
// Comparison
131+
.def (py::self == py::self)
132+
.def (py::self != py::self)
133+
.def ("approximatelyEqualTo", &T::approximatelyEqualTo)
134+
135+
// Operators
136+
.def (py::self + py::self)
137+
.def (py::self += py::self)
138+
.def (py::self - py::self)
139+
.def (py::self -= py::self)
140+
.def (py::self * py::self)
141+
.def (py::self *= py::self)
142+
.def (py::self / py::self)
143+
.def (py::self /= py::self)
144+
.def (py::self + ValueType())
145+
.def (py::self += ValueType())
146+
.def (py::self - ValueType())
147+
.def (py::self -= ValueType())
148+
.def (py::self * ValueType())
149+
.def (py::self *= ValueType())
150+
.def (py::self / ValueType())
151+
.def (py::self /= ValueType())
152+
.def (-py::self)
153+
154+
// Add conversion methods
155+
.def ("toInt", [](const T& self) { return self.template to<int>(); })
156+
.def ("toLong", [](const T& self) { return self.template to<long>(); })
157+
.def ("toFloat", [](const T& self) { return self.template to<float>(); })
158+
.def ("toDouble", [](const T& self) { return self.template to<double>(); })
159+
160+
// Add properties for more pythonic access
161+
.def_property("x", &T::getX, &T::setX)
162+
.def_property("y", &T::getY, &T::setY)
163+
164+
.def ("__repr__", [](const T& self)
165+
{
166+
String result;
167+
result
168+
<< Helpers::pythonizeModuleClassName (PythonModuleName, typeid (self).name())
169+
<< "(" << self.getX() << ", " << self.getY() << ")";
170+
return result;
171+
})
172+
.def ("__str__", [](const T& self)
173+
{
174+
String result;
175+
result << "(" << self.getX() << ", " << self.getY() << ")";
176+
return result;
177+
})
178+
;
115179

116-
/*
180+
// Add floating-point specific methods
117181
if constexpr (std::is_floating_point_v<ValueType>)
118182
{
119183
class_
120-
.def ("rotatedAboutOrigin", [](const T& self, float angleRadians) { return self.rotatedAboutOrigin (angleRadians); });
184+
.def ("getPointOnCircumference", py::overload_cast<float, float>(&T::template getPointOnCircumference<ValueType>, py::const_))
185+
.def ("getPointOnCircumference", py::overload_cast<float, float, float>(&T::template getPointOnCircumference<ValueType>, py::const_))
186+
.def ("isFinite", [](const T& self) { return self.template isFinite<ValueType>(); })
187+
.def ("floor", [](const T& self) { return self.template floor<ValueType>(); })
188+
.def ("ceil", [](const T& self) { return self.template ceil<ValueType>(); })
189+
.def ("scale", [](T& self, ValueType factor) -> T& { return self.template scale<ValueType>(factor); })
190+
.def ("scale", [](T& self, ValueType factorX, ValueType factorY) -> T& { return self.template scale<ValueType>(factorX, factorY); })
191+
.def ("scaled", [](const T& self, ValueType factor) { return self.template scaled<ValueType>(factor); })
192+
.def ("scaled", [](const T& self, ValueType factorX, ValueType factorY) { return self.template scaled<ValueType>(factorX, factorY); })
193+
.def ("roundToInt", [](const T& self) { return self.template roundToInt<ValueType>(); })
194+
.def ("toNearestInt", [](const T& self) { return self.template toNearestInt<ValueType>(); })
195+
;
121196
}
122-
*/
123197

124198
type[py::type::of (py::cast (Types {}))] = class_;
125199

126200
return true;
127201
}() && ...);
128202

129203
m.add_object ("Point", type);
204+
205+
// clang-format on
130206
}
131207

132208
// ============================================================================================

0 commit comments

Comments
 (0)