Skip to content

Commit 20cd427

Browse files
authored
Merge pull request #31 from andrsd/pt-translate
Adding `Point::translate` by a vector defined by 2 points
2 parents 15b324f + df6d3b7 commit 20cd427

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

include/formo/point.h

+14
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,26 @@ class Point : public Shape {
113113
/// @param vec The vector of translation
114114
void translate(const Vector & vec);
115115

116+
/// Translates a point from point `p1` to point `p2`
117+
///
118+
/// @param p1 First point
119+
/// @param p2 Second point
120+
void translate(const Point & p1, const Point & p2);
121+
116122
/// Translates a point in the direction of the vector `vec`. The magnitude of the translation is
117123
/// the vector's magnitude.
118124
///
119125
/// @param vec The vector of translation
126+
/// @return Translated point
120127
Point translated(const Vector & vec) const;
121128

129+
/// Translates a point from point `p1` to point `p2`
130+
///
131+
/// @param p1 First point
132+
/// @param p2 Second point
133+
/// @return Translated point
134+
Point translated(const Point & p1, const Point & p2) const;
135+
122136
private:
123137
gp_Pnt pnt;
124138
TopoDS_Vertex vtx;

python/src/formo.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,14 @@ PYBIND11_MODULE(formo, m)
179179
py::arg("ax1"), py::arg("angle"))
180180
.def("rotated", &Point::rotated,
181181
py::arg("ax1"), py::arg("angle"))
182-
.def("translate", &Point::translate,
182+
.def("translate", static_cast<void (Point::*)(const Vector &)>(&Point::translate),
183183
py::arg("vec"))
184-
.def("translated", &Point::translated,
184+
.def("translate", static_cast<void (Point::*)(const Point &, const Point &)>(&Point::translate),
185+
py::arg("p1"), py::arg("p2"))
186+
.def("translated", static_cast<Point (Point::*)(const Vector &) const>(&Point::translated),
185187
py::arg("vec"))
188+
.def("translated", static_cast<Point (Point::*)(const Point &, const Point &) const>(&Point::translated),
189+
py::arg("p1"), py::arg("p2"))
186190
;
187191

188192
py::class_<Direction>(m, "Direction")

src/point.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,24 @@ Point::translate(const Vector & vec)
126126
this->pnt.Translate(vec);
127127
}
128128

129+
void
130+
Point::translate(const Point & p1, const Point & p2)
131+
{
132+
this->pnt.Translate(p1, p2);
133+
}
134+
129135
Point
130136
Point::translated(const Vector & vec) const
131137
{
132138
return Point(this->pnt.Translated(vec));
133139
}
134140

141+
Point
142+
Point::translated(const Point & p1, const Point & p2) const
143+
{
144+
return Point(this->pnt.Translated(p1, p2));
145+
}
146+
135147
Point::operator gp_Pnt() const
136148
{
137149
return this->pnt;

test/src/Point_test.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ TEST(PointTest, translate)
9191
EXPECT_NEAR(pt.z(), 4, 1e-15);
9292
}
9393

94+
TEST(PointTest, translate_2pts)
95+
{
96+
Point pt(1, 0, 2);
97+
pt.translate(Point(0, 0, 0), Point(1, -1, 2));
98+
EXPECT_NEAR(pt.x(), 2, 1e-15);
99+
EXPECT_NEAR(pt.y(), -1, 1e-15);
100+
EXPECT_NEAR(pt.z(), 4, 1e-15);
101+
}
102+
94103
TEST(PointTest, translated)
95104
{
96105
Point pt(1, 0, 2);
@@ -100,6 +109,15 @@ TEST(PointTest, translated)
100109
EXPECT_NEAR(npt.z(), 4, 1e-15);
101110
}
102111

112+
TEST(PointTest, translated_2pts)
113+
{
114+
Point pt(1, 0, 2);
115+
auto npt = pt.translated(Point(0, 0, 0), Point(1, -1, 2));
116+
EXPECT_NEAR(npt.x(), 2, 1e-15);
117+
EXPECT_NEAR(npt.y(), -1, 1e-15);
118+
EXPECT_NEAR(npt.z(), 4, 1e-15);
119+
}
120+
103121
TEST(PointTest, mirror_pt)
104122
{
105123
Point pt(1, 0, -2);

0 commit comments

Comments
 (0)