209{
211 const Eigen::ParametrizedLine<float, 3> ray(s, dir.normalized());
212
213 for (size_t i=0; i<2; ++i)
215
216 const float sqr_radius = pow(
radius, 2.f);
217
218
220 float sqr_dist_limit = pow(
height/2.f, 2.f) + sqr_radius ;
221 if (ray.squaredDistance(
center) > sqr_dist_limit)
222 return false;
223
224
225
226
227 size_t found = 0;
228 Eigen::Hyperplane<float, 3> base;
230 for (size_t i=1; i<=1; --i) {
232 if (i == 0) {
233
234
236 }
237 base = Eigen::Hyperplane<float, 3>(
normal, cylinder_center);
239
240 if ((cylinder_center-
intersection).squaredNorm() < sqr_radius) {
241 out[found].first = ray.intersectionParameter(base);
242 out[found].second = (i==0 ? 1. : -1.) *
normal.cast<
double>();
243 ++found;
244 }
245 }
246 }
247 else
248 {
249
250
252 }
253
254
255 if (found != 2 && !
is_approx(std::abs(ray.direction().dot(
normal)), 1.f)) {
256
257 Vec3f proj_origin = base.projection(ray.origin());
258 Vec3f proj_dir = base.projection(ray.origin()+ray.direction())-proj_origin;
259
260 float par_scale = proj_dir.norm();
261 proj_dir = proj_dir/par_scale;
262 Eigen::ParametrizedLine<float, 3> projected_ray(proj_origin, proj_dir);
263
264
266 float dist = sqrt((sqr_radius - (closest-
pos).squaredNorm()));
267
268
269 for (int i=-1; i<=1 && found !=2; i+=2) {
271 Vec3f to_isect = isect-proj_origin;
272 float par = to_isect.norm() / par_scale;
273 if (to_isect.normalized().dot(proj_dir.normalized()) < 0.f)
274 par *= -1.f;
275 Vec3d hit_normal = (
pos-isect).normalized().cast<
double>();
276 isect = ray.pointAt(par);
277
278 float vert_dist = base.signedDistance(isect);
279 if (vert_dist > 0.f && vert_dist <
height) {
280 out[found].first = par;
281 out[found].second = hit_normal;
282 ++found;
283 }
284 }
285 }
286
287
288
289 if (found != 2)
290 return false;
291
292
293 if (out[0].first > out[1].first)
294 std::swap(out[0], out[1]);
295
296 return true;
297}
#define EPSILON
Definition ColorUtils.cpp:64
static constexpr double infty()
Definition IndexedMesh.hpp:93
T dist(const boost::polygon::point_data< T > &p1, const boost::polygon::point_data< T > &p2)
Definition Geometry.cpp:298
Foot closest(const Contour &c, const Vec2d &p, double stop=-1.0, const std::vector< Vec2d > *source_normals=nullptr, double normal_cosine=0.0)
Definition SeamContour.cpp:36
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vec3d
Definition SpatIndex.hpp:18
void center(ExPolygonsWithIds &e)
Definition Emboss.cpp:1382
Slic3r::Polygons intersection(const Slic3r::Polygon &subject, const Slic3r::Polygon &clip, ApplySafetyOffset do_safety_offset)
Definition ClipperUtils.cpp:769
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vec3f
Definition Point.hpp:65
constexpr bool is_approx(Number value, Number test_value, Number precision=EPSILON)
Definition libslic3r.h:320