Pointf3 FarthestAxis(Pointf3 v) { double tx = fabs(v.x), ty = fabs(v.y), tz = fabs(v.z); return tx <= ty && tx <= tz ? Pointf3(1, 0, 0) : ty <= tx && ty <= tz ? Pointf3(0, 1, 0) : Pointf3(0, 0, 1); }
BoundingBoxf3 ModelInstance::transform_bounding_box(const BoundingBoxf3 &bbox, bool dont_translate) const { // rotate around mesh origin double c = cos(this->rotation); double s = sin(this->rotation); Pointf3 pts[4] = { bbox.min, bbox.max, Pointf3(bbox.min.x, bbox.max.y, bbox.min.z), Pointf3(bbox.max.x, bbox.min.y, bbox.max.z) }; BoundingBoxf3 out; for (int i = 0; i < 4; ++ i) { Pointf3 &v = pts[i]; double xold = v.x; double yold = v.y; v.x = float(c * xold - s * yold); v.y = float(s * xold + c * yold); v.x *= this->scaling_factor; v.y *= this->scaling_factor; v.z *= this->scaling_factor; if (!dont_translate) { v.x += this->offset.x; v.y += this->offset.y; } out.merge(v); } return out; }
Camera::Camera() { ViewingAngle(120 * DEGRAD); // default viewing angle in radians Location(Pointf3(-100, 100, 0)); // 150 m from origin in the -x+y distance Target(Pointf3(0, 0, 0)); Upwards(Pointf3(0, 1, 0)); FarDistance(10000); // 10 km NearDistance(100); // 100 m Viewport(320, 240); ViewportOffset(0, 0); ViewportSize(1, 1); Projection(Matrixf3_1()); Update(); }
BoundingBoxf3 ModelInstance::transform_mesh_bounding_box(const TriangleMesh* mesh, bool dont_translate) const { // rotate around mesh origin double c = cos(this->rotation); double s = sin(this->rotation); BoundingBoxf3 bbox; for (int i = 0; i < mesh->stl.stats.number_of_facets; ++ i) { const stl_facet &facet = mesh->stl.facet_start[i]; for (int j = 0; j < 3; ++ j) { stl_vertex v = facet.vertex[j]; double xold = v.x; double yold = v.y; v.x = float(c * xold - s * yold); v.y = float(s * xold + c * yold); v.x *= float(this->scaling_factor); v.y *= float(this->scaling_factor); v.z *= float(this->scaling_factor); if (!dont_translate) { v.x += this->offset.x; v.y += this->offset.y; } bbox.merge(Pointf3(v.x, v.y, v.z)); } } return bbox; }
Pointf3 Camera::Transform(Pointf3 point) const { Pointf3 axon = point * transform_matrix; static const double MIN_DISTANCE = 1e-3; // 1 mm should be good enough if(axon.z >= -MIN_DISTANCE && axon.z <= +MIN_DISTANCE) axon.z = MIN_DISTANCE; return Pointf3(axon.x / axon.z, axon.y / axon.z, axon.z + z_delta); }
Pointf3 Linef3::intersect_plane(double z) const { return Pointf3( this->a.x + (this->b.x - this->a.x) * (z - this->a.z) / (this->b.z - this->a.z), this->a.y + (this->b.y - this->a.y) * (z - this->a.z) / (this->b.z - this->a.z), z ); }
void ModelObject::mirror(const Axis &axis) { for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { (*v)->mesh.mirror(axis); } this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); }
Matrixf3 Matrixf3Inverse(const Matrixf3& mx) { double det = Determinant(mx); Matrixf3 m( Pointf3( (mx.y.y * mx.z.z - mx.z.y * mx.y.z), (mx.z.y * mx.x.z - mx.x.y * mx.z.z), (mx.x.y * mx.y.z - mx.y.y * mx.x.z)) / det, Pointf3( (mx.y.z * mx.z.x - mx.z.z * mx.y.x), (mx.z.z * mx.x.x - mx.x.z * mx.z.x), (mx.x.z * mx.y.x - mx.y.z * mx.x.x)) / det, Pointf3( (mx.y.x * mx.z.y - mx.z.x * mx.y.y), (mx.z.x * mx.x.y - mx.x.x * mx.z.y), (mx.x.x * mx.y.y - mx.y.x * mx.x.y)) / det); m.a = -mx.a % m; return m; }
void ModelObject::scale(const Pointf3 &versor) { for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { (*v)->mesh.scale(versor); } // reset origin translation since it doesn't make sense anymore this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); }
void Camera::Update() { direction = Unit(target - location); distance_delta = -(location ^ direction); viewing_distance = double(min(width_mm, height_mm) / (2e3 * tan(viewing_angle / 2.0))); Pointf3 up = Orthogonal(upwards, direction); if(Squared(up) <= 1e-10) up = Orthogonal(FarthestAxis(direction), direction); straight_up = Unit(up); straight_right = direction % straight_up; camera_matrix = Matrixf3(straight_right, straight_up, direction, location); invcam_matrix = Matrixf3Inverse(camera_matrix); double dw = viewing_distance * view_px * 2e3 / width_mm; double dh = viewing_distance * view_py * 2e3 / height_mm; double z_times = far_distance - near_distance; z_delta = -near_distance / z_times; transform_matrix = invcam_matrix * Matrixf3( Pointf3(dw / z_times, 0, 0), Pointf3(0, dh / z_times, 0), Pointf3(shift_x, shift_y, 1) / z_times); }
void Camera::SetPolar(Pointf3 dz, Pointf3 dx, double d, double a, double b, double r) { Pointf3 dy = dz % dx; Pointf3 az = RotateX(Pointf3(0, 0, -1), -b); az = d * RotateY(az, a); Pointf3 loc = az * Matrixf3(dx, dy, dz, target); Pointf3 dir = Unit(target - loc); Pointf3 su = Orthogonal(dy, dir); if(Length(su) <= 1e-10) su = Orthogonal(FarthestAxis(dir), dir); su = Unit(su); Location(loc).Upwards(Rotate(su, dir, r)); }
Pointf3 Camera::InverseXY(Pointf point, double z) const { double dax = point.x * transform_matrix.x.z - transform_matrix.x.x; double day = point.x * transform_matrix.y.z - transform_matrix.y.x; double dar = z * transform_matrix.z.x + transform_matrix.a.x - point.x * (z * transform_matrix.z.z + transform_matrix.a.z); double dbx = point.y * transform_matrix.x.z - transform_matrix.x.y; double dby = point.y * transform_matrix.y.z - transform_matrix.y.y; double dbr = z * transform_matrix.z.y + transform_matrix.a.y - point.y * (z * transform_matrix.z.z + transform_matrix.a.z); double dx = dar * dby - dbr * day; double dy = dax * dbr - dar * dbx; double dr = dax * dby - dbx * day; double lim = fabs(dr) * 1e6; if(lim <= fabs(dx) || lim <= fabs(dy)) return Null; return Pointf3(dx / dr, dy / dr, z); }
Pointf3 RotateZ(Pointf3 v, double angle, Pointf3 centre) { double c = cos(angle), s = sin(angle), x = v.x - centre.x, y = v.y - centre.y; return Pointf3(x * c - y * s + centre.x, x * s + y * c + centre.y, v.z); }
Pointf3 RotateY(Pointf3 v, double angle, Pointf3 centre) { double c = cos(angle), s = sin(angle), z = v.z - centre.z, x = v.x - centre.x; return Pointf3(z * s + x * c + centre.x, v.y, z * c - x * s + centre.z); }
Pointf3 RotateX(Pointf3 v, double angle, Pointf3 centre) { double c = cos(angle), s = sin(angle), y = v.y - centre.y, z = v.z - centre.z; return Pointf3(v.x, y * c - z * s + centre.y, y * s + z * c + centre.z); }
Pointf3 Pointf3::negative() const { return Pointf3(-this->x, -this->y, -this->z); }
static Pointf3 new_unscale(coord_t x, coord_t y, coord_t z) { return Pointf3(unscale(x), unscale(y), unscale(z)); };
Matrixf3 Matrixf3Scale(Pointf3 scale, Pointf3 fix) { return Matrixf3(Pointf3(scale.x, 0, 0), Pointf3(0, scale.y, 0), Pointf3(0, 0, scale.z), fix - scale * fix); }
Matrixf3 Matrixf3Scale(double scale, Pointf3 fix) { return Matrixf3(Pointf3(scale, 0, 0), Pointf3(0, scale, 0), Pointf3(0, 0, scale), fix * (1 - scale)); }
Matrixf3 Matrixf3Move(Pointf3 vector) { return Matrixf3(Pointf3(1, 0, 0), Pointf3(0, 1, 0), Pointf3(0, 0, 1), vector); }