Trait fj_kernel::algorithms::transform::TransformObject
source · pub trait TransformObject: Sized {
fn transform_with_cache(
self,
transform: &Transform,
objects: &mut Service<Objects>,
cache: &mut TransformCache
) -> Self;
fn transform(
self,
transform: &Transform,
objects: &mut Service<Objects>
) -> Self { ... }
fn translate(
self,
offset: impl Into<Vector<3>>,
objects: &mut Service<Objects>
) -> Self { ... }
fn rotate(
self,
axis_angle: impl Into<Vector<3>>,
objects: &mut Service<Objects>
) -> Self { ... }
}Expand description
Transform an object
Implementation Note
So far, a general transform method is available, along some convenience
methods for more specific transformations.
More convenience methods can be added as required. The only reason this hasn’t been done so far, is that no one has put in the work yet.
Required Methods§
sourcefn transform_with_cache(
self,
transform: &Transform,
objects: &mut Service<Objects>,
cache: &mut TransformCache
) -> Self
fn transform_with_cache(
self,
transform: &Transform,
objects: &mut Service<Objects>,
cache: &mut TransformCache
) -> Self
Transform the object using the provided cache
Provided Methods§
sourcefn transform(self, transform: &Transform, objects: &mut Service<Objects>) -> Self
fn transform(self, transform: &Transform, objects: &mut Service<Objects>) -> Self
Transform the object
Examples found in repository?
src/algorithms/transform/mod.rs (line 61)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
fn translate(
self,
offset: impl Into<Vector<3>>,
objects: &mut Service<Objects>,
) -> Self {
self.transform(&Transform::translation(offset), objects)
}
/// Rotate the object
///
/// Convenience wrapper around [`TransformObject::transform`].
fn rotate(
self,
axis_angle: impl Into<Vector<3>>,
objects: &mut Service<Objects>,
) -> Self {
self.transform(&Transform::rotation(axis_angle), objects)
}sourcefn translate(
self,
offset: impl Into<Vector<3>>,
objects: &mut Service<Objects>
) -> Self
fn translate(
self,
offset: impl Into<Vector<3>>,
objects: &mut Service<Objects>
) -> Self
Translate the object
Convenience wrapper around TransformObject::transform.
Examples found in repository?
src/algorithms/sweep/face.rs (line 53)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
fn sweep_with_cache(
self,
path: impl Into<Vector<3>>,
cache: &mut SweepCache,
objects: &mut Service<Objects>,
) -> Self::Swept {
let path = path.into();
let mut faces = Vec::new();
let is_negative_sweep = {
let u = match self.surface().geometry().u {
GlobalPath::Circle(_) => todo!(
"Sweeping from faces defined in round surfaces is not \
supported"
),
GlobalPath::Line(line) => line.direction(),
};
let v = self.surface().geometry().v;
let normal = u.cross(&v);
normal.dot(&path) < Scalar::ZERO
};
let bottom_face = {
if is_negative_sweep {
self.clone()
} else {
self.clone().reverse(objects)
}
};
faces.push(bottom_face);
let top_face = {
let mut face = self.clone().translate(path, objects);
if is_negative_sweep {
face = face.reverse(objects);
};
face
};
faces.push(top_face);
// Generate side faces
for cycle in self.all_cycles() {
for half_edge in cycle.half_edges() {
let half_edge = if is_negative_sweep {
half_edge.clone().reverse(objects)
} else {
half_edge.clone()
};
let face = (half_edge, self.color())
.sweep_with_cache(path, cache, objects);
faces.push(face);
}
}
let faces = faces.into_iter().map(Partial::from).collect();
PartialShell { faces }.build(objects).insert(objects)
}More examples
src/algorithms/sweep/edge.rs (line 113)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
fn sweep_with_cache(
self,
path: impl Into<Vector<3>>,
cache: &mut SweepCache,
objects: &mut Service<Objects>,
) -> Self::Swept {
let (edge, color) = self;
let path = path.into();
let surface =
edge.curve().clone().sweep_with_cache(path, cache, objects);
// We can't use the edge we're sweeping from as the bottom edge, as that
// is not defined in the right surface. Let's create a new bottom edge,
// by swapping the surface of the original.
let bottom_edge = {
let vertices = edge.vertices();
let points_curve_and_surface = vertices.clone().map(|vertex| {
(vertex.position(), [vertex.position().t, Scalar::ZERO])
});
let curve = {
// Please note that creating a line here is correct, even if the
// global curve is a circle. Projected into the side surface, it
// is going to be a line either way.
let path =
SurfacePath::Line(Line::from_points_with_line_coords(
points_curve_and_surface,
));
Curve::new(
surface.clone(),
path,
edge.curve().global_form().clone(),
)
.insert(objects)
};
let vertices = {
let points_surface = points_curve_and_surface
.map(|(_, point_surface)| point_surface);
vertices
.each_ref_ext()
.into_iter_fixed()
.zip(points_surface)
.collect::<[_; 2]>()
.map(|(vertex, point_surface)| {
let surface_vertex = SurfaceVertex::new(
point_surface,
surface.clone(),
vertex.global_form().clone(),
)
.insert(objects);
Vertex::new(
vertex.position(),
curve.clone(),
surface_vertex,
)
.insert(objects)
})
};
HalfEdge::new(vertices, edge.global_form().clone()).insert(objects)
};
let side_edges = bottom_edge.vertices().clone().map(|vertex| {
(vertex, surface.clone()).sweep_with_cache(path, cache, objects)
});
let top_edge = {
let bottom_vertices = bottom_edge.vertices();
let surface_vertices = side_edges.clone().map(|edge| {
let [_, vertex] = edge.vertices();
vertex.surface_form().clone()
});
let points_curve_and_surface =
bottom_vertices.clone().map(|vertex| {
(vertex.position(), [vertex.position().t, Scalar::ONE])
});
let curve = {
let global = bottom_edge
.curve()
.global_form()
.clone()
.translate(path, objects);
// Please note that creating a line here is correct, even if the
// global curve is a circle. Projected into the side surface, it
// is going to be a line either way.
let path =
SurfacePath::Line(Line::from_points_with_line_coords(
points_curve_and_surface,
));
Curve::new(surface, path, global).insert(objects)
};
let global = GlobalEdge::new(
curve.global_form().clone(),
surface_vertices
.clone()
.map(|surface_vertex| surface_vertex.global_form().clone()),
)
.insert(objects);
let vertices = bottom_vertices
.each_ref_ext()
.into_iter_fixed()
.zip(surface_vertices)
.collect::<[_; 2]>()
.map(|(vertex, surface_form)| {
Vertex::new(vertex.position(), curve.clone(), surface_form)
.insert(objects)
});
HalfEdge::new(vertices, global).insert(objects)
};
let cycle = {
let a = bottom_edge;
let [d, b] = side_edges;
let c = top_edge;
let mut edges = [a, b, c, d];
// Make sure that edges are oriented correctly.
let mut i = 0;
while i < edges.len() {
let j = (i + 1) % edges.len();
let [_, prev_last] = edges[i].vertices();
let [next_first, _] = edges[j].vertices();
// Need to compare surface forms here, as the global forms might
// be coincident when sweeping circles, despite the vertices
// being different!
if prev_last.surface_form().id()
!= next_first.surface_form().id()
{
edges[j] = edges[j].clone().reverse(objects);
}
i += 1;
}
Cycle::new(edges).insert(objects)
};
let face = PartialFace {
exterior: Partial::from(cycle),
color: Some(color),
..Default::default()
};
face.build(objects).insert(objects)
}src/builder/shell.rs (line 42)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
fn create_cube_from_edge_length(
edge_length: impl Into<Scalar>,
objects: &mut Service<Objects>,
) -> Self {
let edge_length = edge_length.into();
// Let's define some short-hands. We're going to need them a lot.
const Z: Scalar = Scalar::ZERO;
let h = edge_length / 2.;
let bottom_face = {
let surface =
objects.surfaces.xy_plane().translate([Z, Z, -h], objects);
let mut face = PartialFace::default();
face.update_exterior_as_polygon(
surface,
[[-h, -h], [h, -h], [h, h], [-h, h]],
);
face
};
let (side_faces, top_edges) = {
let side_surfaces = bottom_face
.exterior
.read()
.half_edges
.iter()
.map(|half_edge| {
let [a, b] =
half_edge.read().vertices.clone().map(|mut vertex| {
vertex
.write()
.surface_form
.write()
.infer_global_position()
});
let c = a + [Z, Z, edge_length];
let (surface, _) =
PartialSurface::plane_from_points([a, b, c]);
Partial::from_partial(surface)
})
.collect::<Vec<_>>();
let bottom_edges = bottom_face
.exterior
.read()
.half_edges
.iter()
.zip(&side_surfaces)
.map(|(half_edge, surface)| {
let global_edge = half_edge.read().global_form.clone();
let mut half_edge = PartialHalfEdge::default();
half_edge.curve().write().global_form =
global_edge.read().curve.clone();
for (vertex, global_form) in half_edge
.vertices
.iter_mut()
.zip(&global_edge.read().vertices)
{
vertex.write().surface_form.write().global_form =
global_form.clone();
}
half_edge.global_form = global_edge;
half_edge.update_as_line_segment_from_points(
surface.clone(),
[[Z, Z], [edge_length, Z]],
);
Partial::from_partial(half_edge)
})
.collect::<Vec<_>>();
let side_edges_up = bottom_edges
.clone()
.into_iter()
.zip(&side_surfaces)
.map(|(bottom, surface): (Partial<HalfEdge>, _)| {
let from_surface = {
let [_, from] = &bottom.read().vertices;
let from = from.read();
from.surface_form.clone()
};
let to_position = from_surface.read().position.unwrap()
+ [Z, edge_length];
let mut half_edge = PartialHalfEdge::default();
half_edge.curve().write().surface = surface.clone();
{
let [from, to] = &mut half_edge.vertices;
from.write().surface_form = from_surface;
let mut to = to.write();
let mut to_surface = to.surface_form.write();
to_surface.position = Some(to_position);
to_surface.surface = surface.clone();
}
half_edge.infer_global_form();
half_edge.update_as_line_segment();
Partial::from_partial(half_edge)
})
.collect::<Vec<_>>();
let side_edges_down = {
let mut sides_up_prev = side_edges_up.clone();
sides_up_prev.rotate_right(1);
bottom_edges
.clone()
.into_iter()
.zip(sides_up_prev)
.zip(&side_surfaces)
.map(
|((bottom, side_up_prev), surface): (
(_, Partial<HalfEdge>),
_,
)| {
let [_, from] =
side_up_prev.read().vertices.clone();
let [to, _] = bottom.read().vertices.clone();
let from_global = from
.read()
.surface_form
.read()
.global_form
.clone();
let to_surface = to.read().surface_form.clone();
let mut half_edge = PartialHalfEdge::default();
half_edge.curve().write().surface = surface.clone();
half_edge.curve().write().global_form =
side_up_prev
.read()
.curve()
.read()
.global_form
.clone();
{
let [from, to] = &mut half_edge.vertices;
let mut from = from.write();
let mut from_surface =
from.surface_form.write();
from_surface.position = Some(
to_surface.read().position.unwrap()
+ [Z, edge_length],
);
from_surface.surface = surface.clone();
from_surface.global_form = from_global;
to.write().surface_form = to_surface;
}
half_edge.infer_global_form();
half_edge.update_as_line_segment();
Partial::from_partial(half_edge)
},
)
.collect::<Vec<_>>()
};
let top_edges = side_edges_up
.clone()
.into_iter()
.zip(side_edges_down.clone())
.map(|(side_up, side_down): (_, Partial<HalfEdge>)| {
let [_, from] = side_up.read().vertices.clone();
let [to, _] = side_down.read().vertices.clone();
let from_surface = from.read().surface_form.clone();
let to_surface = to.read().surface_form.clone();
let mut half_edge = PartialHalfEdge::default();
half_edge.curve().write().surface =
from_surface.read().surface.clone();
half_edge.global_form.write().vertices = [
from_surface.read().global_form.clone(),
to_surface.read().global_form.clone(),
];
{
let [from, to] = &mut half_edge.vertices;
from.write().surface_form = from_surface;
to.write().surface_form = to_surface;
}
half_edge.update_as_line_segment();
Partial::from_partial(half_edge)
})
.collect::<Vec<_>>();
let side_faces = bottom_edges
.into_iter()
.zip(side_edges_up)
.zip(top_edges.clone())
.zip(side_edges_down)
.map(|(((bottom, side_up), top), side_down)| {
let mut cycle = PartialCycle::default();
cycle.half_edges.extend([bottom, side_up, top, side_down]);
PartialFace {
exterior: Partial::from_partial(cycle),
..Default::default()
}
})
.collect::<Vec<_>>();
(side_faces, top_edges)
};
let top_face = {
let surface = Partial::from(
objects.surfaces.xy_plane().translate([Z, Z, h], objects),
);
let mut top_edges = top_edges;
top_edges.reverse();
let surface_vertices = {
let points = [[-h, -h], [-h, h], [h, h], [h, -h]];
let mut edges = top_edges.iter();
let half_edges = array::from_fn(|_| edges.next().unwrap());
let [a, b, c, d] = points
.into_iter_fixed()
.zip(half_edges)
.collect::<[_; 4]>()
.map(|(point, edge)| {
let [vertex, _] = edge.read().vertices.clone();
let global_vertex = vertex
.read()
.surface_form
.read()
.global_form
.clone();
Partial::from_partial(PartialSurfaceVertex {
position: Some(point.into()),
surface: surface.clone(),
global_form: global_vertex,
})
});
[a.clone(), b, c, d, a]
};
let mut half_edges = Vec::new();
for (surface_vertices, edge) in surface_vertices
.as_slice()
.array_windows_ext()
.zip(top_edges)
{
let global_form = edge.read().global_form.clone();
let mut half_edge = PartialHalfEdge::default();
half_edge.curve().write().surface = surface.clone();
half_edge.curve().write().global_form =
global_form.read().curve.clone();
half_edge.global_form = global_form;
for (vertex, surface_form) in half_edge
.vertices
.each_mut_ext()
.zip_ext(surface_vertices.each_ref_ext())
{
vertex.write().surface_form = surface_form.clone();
}
half_edge.update_as_line_segment();
half_edges.push(Partial::from_partial(half_edge));
}
PartialFace {
exterior: Partial::from_partial(PartialCycle { half_edges }),
..Default::default()
}
};
PartialShell {
faces: [bottom_face]
.into_iter()
.chain(side_faces)
.chain([top_face])
.map(Partial::from_partial)
.collect(),
}
}