Struct truck_polymesh::StructuredMesh
source · pub struct StructuredMesh { /* private fields */ }
Expand description
structured quadrangle mesh
Implementations§
source§impl StructuredMesh
impl StructuredMesh
sourcepub fn from_surface<S>(
surface: &S,
range: ((f64, f64), (f64, f64)),
tol: f64
) -> StructuredMeshwhere
S: ParametricSurface3D + ParameterDivision2D,
pub fn from_surface<S>(
surface: &S,
range: ((f64, f64), (f64, f64)),
tol: f64
) -> StructuredMeshwhere
S: ParametricSurface3D + ParameterDivision2D,
meshing the surface
Arguments
bspsurface
- bspline surface to meshedtol
- standard tolerance for meshing
source§impl StructuredMesh
impl StructuredMesh
sourcepub fn from_positions(positions: Vec<Vec<Point3>>) -> StructuredMesh
pub fn from_positions(positions: Vec<Vec<Point3>>) -> StructuredMesh
Creates a structured polygon without uv_division
and normal
.
sourcepub fn try_from_positions(
positions: Vec<Vec<Point3>>
) -> Result<StructuredMesh, Error>
pub fn try_from_positions(
positions: Vec<Vec<Point3>>
) -> Result<StructuredMesh, Error>
Creates a structured polygon without uv_division
and normal
.
Examples found in repository?
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 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
pub fn from_positions(positions: Vec<Vec<Point3>>) -> StructuredMesh {
StructuredMesh::try_from_positions(positions).unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates a structured polygon without `uv_division` and `normal`.
#[inline(always)]
pub fn try_from_positions(positions: Vec<Vec<Point3>>) -> Result<StructuredMesh> {
check_matrix_regularity(&positions)?;
Ok(StructuredMesh::from_positions_unchecked(positions))
}
/// Creates a structured polygon without `uv_division` and `normal`.
#[inline(always)]
pub const fn from_positions_unchecked(positions: Vec<Vec<Point3>>) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: None,
normals: None,
}
}
/// Creates a structured polygon without normals.
#[inline(always)]
pub fn from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
) -> StructuredMesh {
StructuredMesh::try_from_positions_and_uvs(positions, (u_div, v_div))
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates a structured polygon without normals.
#[inline(always)]
pub fn try_from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
) -> Result<StructuredMesh> {
check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
check_vectors_regularity(&u_div, &v_div)?;
Ok(StructuredMesh::from_positions_and_uvs_unchecked(
positions,
(u_div, v_div),
))
}
/// Creates a structured polygon without normals.
#[inline(always)]
pub const fn from_positions_and_uvs_unchecked(
positions: Vec<Vec<Point3>>,
uv_divisions: (Vec<f64>, Vec<f64>),
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: Some(uv_divisions),
normals: None,
}
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub fn from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh::try_from_positions_and_normals(positions, normals)
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub fn try_from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_regularity(&positions)?;
check_matrices_compatibility(&positions, &normals)?;
Ok(StructuredMesh::from_positions_and_normals_unchecked(
positions, normals,
))
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub const fn from_positions_and_normals_unchecked(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: None,
normals: Some(normals),
}
}
/// Creates new structured mesh.
/// Checks whether the size of vectors are compatible before creation.
#[inline(always)]
pub fn new(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh::try_new(positions, uv_division, normals)
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates new structured mesh.
/// Checks whether the size of vectors are compatible before creation.
#[inline(always)]
pub fn try_new(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
check_matrix_vectors_compatibility(&normals, &u_div, &v_div)?;
check_vectors_regularity(&u_div, &v_div)?;
Ok(StructuredMesh::new_unchecked(
positions,
(u_div, v_div),
normals,
))
}
/// Creates new structured mesh.
/// Does not check whether the size of vectors are compatible before creation.
#[inline(always)]
pub const fn new_unchecked(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: Some(uv_division),
normals: Some(normals),
}
}
/// Returns the matrix of all positions.
#[inline(always)]
pub const fn positions(&self) -> &Vec<Vec<Point3>> { &self.positions }
/// Returns the vector of the mutable references to the rows of the positions matrix.
#[inline(always)]
pub fn positions_mut(&mut self) -> Vec<&mut [Point3]> {
self.positions.iter_mut().map(|arr| arr.as_mut()).collect()
}
/// Returns the divisions of uv coordinates.
#[inline(always)]
pub fn uv_division(&self) -> Option<(&Vec<f64>, &Vec<f64>)> {
self.uv_division.as_ref().map(|tuple| (&tuple.0, &tuple.1))
}
/// Returns the mutable slice of uv coordinates division.
#[inline(always)]
pub fn uv_division_mut(&mut self) -> Option<(&mut [f64], &mut [f64])> {
self.uv_division
.as_mut()
.map(|tuple| (tuple.0.as_mut(), tuple.1.as_mut()))
}
/// Returns the matrix of all normals.
#[inline(always)]
pub const fn normals(&self) -> Option<&Vec<Vec<Vector3>>> { self.normals.as_ref() }
/// Returns the vector of the mutable references to the rows of the normals matrix.
#[inline(always)]
pub fn normals_mut(&mut self) -> Option<Vec<&mut [Vector3]>> {
self.normals
.as_mut()
.map(|normals| normals.iter_mut().map(|arr| arr.as_mut()).collect())
}
/// Creates new polygon mesh by destructing `self`.
#[inline(always)]
pub fn destruct(self) -> PolygonMesh {
let StructuredMesh {
positions,
uv_division,
normals,
} = self;
let m = positions.len();
let n = positions[0].len();
let positions = positions.into_iter().flatten().collect();
let uv_coords = uv_division
.map(move |(udiv, vdiv)| {
udiv.into_iter()
.flat_map(|u| vdiv.iter().map(move |v| Vector2::new(u, *v)))
.collect()
})
.unwrap_or_else(Vec::new);
let normals = normals
.map(|n| n.into_iter().flatten().collect())
.unwrap_or_else(Vec::new);
let uv = !uv_coords.is_empty();
let nor = !normals.is_empty();
let quad_faces: Vec<_> = (1..m)
.flat_map(|i| (1..n).map(move |j| (i, j)))
.map(move |(i, j)| {
[
StandardVertex::tuple((i - 1) * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j, uv, nor),
StandardVertex::tuple((i - 1) * n + j, uv, nor),
]
})
.collect();
let faces = Faces {
quad_faces,
..Default::default()
};
PolygonMesh {
attributes: StandardAttributes {
positions,
uv_coords,
normals,
},
faces,
}
}
}
impl<'de> Deserialize<'de> for StructuredMesh {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where D: serde::Deserializer<'de> {
#[derive(Deserialize)]
pub struct StructuredMesh_ {
positions: Vec<Vec<Point3>>,
uv_division: Option<(Vec<f64>, Vec<f64>)>,
normals: Option<Vec<Vec<Vector3>>>,
}
let StructuredMesh_ {
positions,
uv_division,
normals,
} = StructuredMesh_::deserialize(deserializer)?;
match (uv_division, normals) {
(Some(uv_division), Some(normals)) => Self::try_new(positions, uv_division, normals),
(Some(uv_division), None) => Self::try_from_positions_and_uvs(positions, uv_division),
(None, Some(normals)) => Self::try_from_positions_and_normals(positions, normals),
(None, None) => Self::try_from_positions(positions),
}
.map_err(serde::de::Error::custom)
}
sourcepub const fn from_positions_unchecked(
positions: Vec<Vec<Point3>>
) -> StructuredMesh
pub const fn from_positions_unchecked(
positions: Vec<Vec<Point3>>
) -> StructuredMesh
Creates a structured polygon without uv_division
and normal
.
sourcepub fn from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>)
) -> StructuredMesh
pub fn from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>)
) -> StructuredMesh
Creates a structured polygon without normals.
sourcepub fn try_from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>)
) -> Result<StructuredMesh, Error>
pub fn try_from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>)
) -> Result<StructuredMesh, Error>
Creates a structured polygon without normals.
Examples found in repository?
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
pub fn from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
) -> StructuredMesh {
StructuredMesh::try_from_positions_and_uvs(positions, (u_div, v_div))
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates a structured polygon without normals.
#[inline(always)]
pub fn try_from_positions_and_uvs(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
) -> Result<StructuredMesh> {
check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
check_vectors_regularity(&u_div, &v_div)?;
Ok(StructuredMesh::from_positions_and_uvs_unchecked(
positions,
(u_div, v_div),
))
}
/// Creates a structured polygon without normals.
#[inline(always)]
pub const fn from_positions_and_uvs_unchecked(
positions: Vec<Vec<Point3>>,
uv_divisions: (Vec<f64>, Vec<f64>),
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: Some(uv_divisions),
normals: None,
}
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub fn from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh::try_from_positions_and_normals(positions, normals)
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub fn try_from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_regularity(&positions)?;
check_matrices_compatibility(&positions, &normals)?;
Ok(StructuredMesh::from_positions_and_normals_unchecked(
positions, normals,
))
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub const fn from_positions_and_normals_unchecked(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: None,
normals: Some(normals),
}
}
/// Creates new structured mesh.
/// Checks whether the size of vectors are compatible before creation.
#[inline(always)]
pub fn new(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh::try_new(positions, uv_division, normals)
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates new structured mesh.
/// Checks whether the size of vectors are compatible before creation.
#[inline(always)]
pub fn try_new(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
check_matrix_vectors_compatibility(&normals, &u_div, &v_div)?;
check_vectors_regularity(&u_div, &v_div)?;
Ok(StructuredMesh::new_unchecked(
positions,
(u_div, v_div),
normals,
))
}
/// Creates new structured mesh.
/// Does not check whether the size of vectors are compatible before creation.
#[inline(always)]
pub const fn new_unchecked(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: Some(uv_division),
normals: Some(normals),
}
}
/// Returns the matrix of all positions.
#[inline(always)]
pub const fn positions(&self) -> &Vec<Vec<Point3>> { &self.positions }
/// Returns the vector of the mutable references to the rows of the positions matrix.
#[inline(always)]
pub fn positions_mut(&mut self) -> Vec<&mut [Point3]> {
self.positions.iter_mut().map(|arr| arr.as_mut()).collect()
}
/// Returns the divisions of uv coordinates.
#[inline(always)]
pub fn uv_division(&self) -> Option<(&Vec<f64>, &Vec<f64>)> {
self.uv_division.as_ref().map(|tuple| (&tuple.0, &tuple.1))
}
/// Returns the mutable slice of uv coordinates division.
#[inline(always)]
pub fn uv_division_mut(&mut self) -> Option<(&mut [f64], &mut [f64])> {
self.uv_division
.as_mut()
.map(|tuple| (tuple.0.as_mut(), tuple.1.as_mut()))
}
/// Returns the matrix of all normals.
#[inline(always)]
pub const fn normals(&self) -> Option<&Vec<Vec<Vector3>>> { self.normals.as_ref() }
/// Returns the vector of the mutable references to the rows of the normals matrix.
#[inline(always)]
pub fn normals_mut(&mut self) -> Option<Vec<&mut [Vector3]>> {
self.normals
.as_mut()
.map(|normals| normals.iter_mut().map(|arr| arr.as_mut()).collect())
}
/// Creates new polygon mesh by destructing `self`.
#[inline(always)]
pub fn destruct(self) -> PolygonMesh {
let StructuredMesh {
positions,
uv_division,
normals,
} = self;
let m = positions.len();
let n = positions[0].len();
let positions = positions.into_iter().flatten().collect();
let uv_coords = uv_division
.map(move |(udiv, vdiv)| {
udiv.into_iter()
.flat_map(|u| vdiv.iter().map(move |v| Vector2::new(u, *v)))
.collect()
})
.unwrap_or_else(Vec::new);
let normals = normals
.map(|n| n.into_iter().flatten().collect())
.unwrap_or_else(Vec::new);
let uv = !uv_coords.is_empty();
let nor = !normals.is_empty();
let quad_faces: Vec<_> = (1..m)
.flat_map(|i| (1..n).map(move |j| (i, j)))
.map(move |(i, j)| {
[
StandardVertex::tuple((i - 1) * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j, uv, nor),
StandardVertex::tuple((i - 1) * n + j, uv, nor),
]
})
.collect();
let faces = Faces {
quad_faces,
..Default::default()
};
PolygonMesh {
attributes: StandardAttributes {
positions,
uv_coords,
normals,
},
faces,
}
}
}
impl<'de> Deserialize<'de> for StructuredMesh {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where D: serde::Deserializer<'de> {
#[derive(Deserialize)]
pub struct StructuredMesh_ {
positions: Vec<Vec<Point3>>,
uv_division: Option<(Vec<f64>, Vec<f64>)>,
normals: Option<Vec<Vec<Vector3>>>,
}
let StructuredMesh_ {
positions,
uv_division,
normals,
} = StructuredMesh_::deserialize(deserializer)?;
match (uv_division, normals) {
(Some(uv_division), Some(normals)) => Self::try_new(positions, uv_division, normals),
(Some(uv_division), None) => Self::try_from_positions_and_uvs(positions, uv_division),
(None, Some(normals)) => Self::try_from_positions_and_normals(positions, normals),
(None, None) => Self::try_from_positions(positions),
}
.map_err(serde::de::Error::custom)
}
sourcepub const fn from_positions_and_uvs_unchecked(
positions: Vec<Vec<Point3>>,
uv_divisions: (Vec<f64>, Vec<f64>)
) -> StructuredMesh
pub const fn from_positions_and_uvs_unchecked(
positions: Vec<Vec<Point3>>,
uv_divisions: (Vec<f64>, Vec<f64>)
) -> StructuredMesh
Creates a structured polygon without normals.
sourcepub fn from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
pub fn from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
Creates a structured polygon without uv divisions.
sourcepub fn try_from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>
) -> Result<StructuredMesh, Error>
pub fn try_from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>
) -> Result<StructuredMesh, Error>
Creates a structured polygon without uv divisions.
Examples found in repository?
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
pub fn from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh::try_from_positions_and_normals(positions, normals)
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub fn try_from_positions_and_normals(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_regularity(&positions)?;
check_matrices_compatibility(&positions, &normals)?;
Ok(StructuredMesh::from_positions_and_normals_unchecked(
positions, normals,
))
}
/// Creates a structured polygon without uv divisions.
#[inline(always)]
pub const fn from_positions_and_normals_unchecked(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: None,
normals: Some(normals),
}
}
/// Creates new structured mesh.
/// Checks whether the size of vectors are compatible before creation.
#[inline(always)]
pub fn new(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh::try_new(positions, uv_division, normals)
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates new structured mesh.
/// Checks whether the size of vectors are compatible before creation.
#[inline(always)]
pub fn try_new(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
check_matrix_vectors_compatibility(&normals, &u_div, &v_div)?;
check_vectors_regularity(&u_div, &v_div)?;
Ok(StructuredMesh::new_unchecked(
positions,
(u_div, v_div),
normals,
))
}
/// Creates new structured mesh.
/// Does not check whether the size of vectors are compatible before creation.
#[inline(always)]
pub const fn new_unchecked(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: Some(uv_division),
normals: Some(normals),
}
}
/// Returns the matrix of all positions.
#[inline(always)]
pub const fn positions(&self) -> &Vec<Vec<Point3>> { &self.positions }
/// Returns the vector of the mutable references to the rows of the positions matrix.
#[inline(always)]
pub fn positions_mut(&mut self) -> Vec<&mut [Point3]> {
self.positions.iter_mut().map(|arr| arr.as_mut()).collect()
}
/// Returns the divisions of uv coordinates.
#[inline(always)]
pub fn uv_division(&self) -> Option<(&Vec<f64>, &Vec<f64>)> {
self.uv_division.as_ref().map(|tuple| (&tuple.0, &tuple.1))
}
/// Returns the mutable slice of uv coordinates division.
#[inline(always)]
pub fn uv_division_mut(&mut self) -> Option<(&mut [f64], &mut [f64])> {
self.uv_division
.as_mut()
.map(|tuple| (tuple.0.as_mut(), tuple.1.as_mut()))
}
/// Returns the matrix of all normals.
#[inline(always)]
pub const fn normals(&self) -> Option<&Vec<Vec<Vector3>>> { self.normals.as_ref() }
/// Returns the vector of the mutable references to the rows of the normals matrix.
#[inline(always)]
pub fn normals_mut(&mut self) -> Option<Vec<&mut [Vector3]>> {
self.normals
.as_mut()
.map(|normals| normals.iter_mut().map(|arr| arr.as_mut()).collect())
}
/// Creates new polygon mesh by destructing `self`.
#[inline(always)]
pub fn destruct(self) -> PolygonMesh {
let StructuredMesh {
positions,
uv_division,
normals,
} = self;
let m = positions.len();
let n = positions[0].len();
let positions = positions.into_iter().flatten().collect();
let uv_coords = uv_division
.map(move |(udiv, vdiv)| {
udiv.into_iter()
.flat_map(|u| vdiv.iter().map(move |v| Vector2::new(u, *v)))
.collect()
})
.unwrap_or_else(Vec::new);
let normals = normals
.map(|n| n.into_iter().flatten().collect())
.unwrap_or_else(Vec::new);
let uv = !uv_coords.is_empty();
let nor = !normals.is_empty();
let quad_faces: Vec<_> = (1..m)
.flat_map(|i| (1..n).map(move |j| (i, j)))
.map(move |(i, j)| {
[
StandardVertex::tuple((i - 1) * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j, uv, nor),
StandardVertex::tuple((i - 1) * n + j, uv, nor),
]
})
.collect();
let faces = Faces {
quad_faces,
..Default::default()
};
PolygonMesh {
attributes: StandardAttributes {
positions,
uv_coords,
normals,
},
faces,
}
}
}
impl<'de> Deserialize<'de> for StructuredMesh {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where D: serde::Deserializer<'de> {
#[derive(Deserialize)]
pub struct StructuredMesh_ {
positions: Vec<Vec<Point3>>,
uv_division: Option<(Vec<f64>, Vec<f64>)>,
normals: Option<Vec<Vec<Vector3>>>,
}
let StructuredMesh_ {
positions,
uv_division,
normals,
} = StructuredMesh_::deserialize(deserializer)?;
match (uv_division, normals) {
(Some(uv_division), Some(normals)) => Self::try_new(positions, uv_division, normals),
(Some(uv_division), None) => Self::try_from_positions_and_uvs(positions, uv_division),
(None, Some(normals)) => Self::try_from_positions_and_normals(positions, normals),
(None, None) => Self::try_from_positions(positions),
}
.map_err(serde::de::Error::custom)
}
sourcepub const fn from_positions_and_normals_unchecked(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
pub const fn from_positions_and_normals_unchecked(
positions: Vec<Vec<Point3>>,
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
Creates a structured polygon without uv divisions.
sourcepub fn new(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
pub fn new(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
Creates new structured mesh. Checks whether the size of vectors are compatible before creation.
sourcepub fn try_new(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>
) -> Result<StructuredMesh, Error>
pub fn try_new(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>
) -> Result<StructuredMesh, Error>
Creates new structured mesh. Checks whether the size of vectors are compatible before creation.
Examples found in repository?
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
pub fn new(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh::try_new(positions, uv_division, normals)
.unwrap_or_else(|e| panic!("{:?}", e))
}
/// Creates new structured mesh.
/// Checks whether the size of vectors are compatible before creation.
#[inline(always)]
pub fn try_new(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
check_matrix_vectors_compatibility(&normals, &u_div, &v_div)?;
check_vectors_regularity(&u_div, &v_div)?;
Ok(StructuredMesh::new_unchecked(
positions,
(u_div, v_div),
normals,
))
}
/// Creates new structured mesh.
/// Does not check whether the size of vectors are compatible before creation.
#[inline(always)]
pub const fn new_unchecked(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> StructuredMesh {
StructuredMesh {
positions,
uv_division: Some(uv_division),
normals: Some(normals),
}
}
/// Returns the matrix of all positions.
#[inline(always)]
pub const fn positions(&self) -> &Vec<Vec<Point3>> { &self.positions }
/// Returns the vector of the mutable references to the rows of the positions matrix.
#[inline(always)]
pub fn positions_mut(&mut self) -> Vec<&mut [Point3]> {
self.positions.iter_mut().map(|arr| arr.as_mut()).collect()
}
/// Returns the divisions of uv coordinates.
#[inline(always)]
pub fn uv_division(&self) -> Option<(&Vec<f64>, &Vec<f64>)> {
self.uv_division.as_ref().map(|tuple| (&tuple.0, &tuple.1))
}
/// Returns the mutable slice of uv coordinates division.
#[inline(always)]
pub fn uv_division_mut(&mut self) -> Option<(&mut [f64], &mut [f64])> {
self.uv_division
.as_mut()
.map(|tuple| (tuple.0.as_mut(), tuple.1.as_mut()))
}
/// Returns the matrix of all normals.
#[inline(always)]
pub const fn normals(&self) -> Option<&Vec<Vec<Vector3>>> { self.normals.as_ref() }
/// Returns the vector of the mutable references to the rows of the normals matrix.
#[inline(always)]
pub fn normals_mut(&mut self) -> Option<Vec<&mut [Vector3]>> {
self.normals
.as_mut()
.map(|normals| normals.iter_mut().map(|arr| arr.as_mut()).collect())
}
/// Creates new polygon mesh by destructing `self`.
#[inline(always)]
pub fn destruct(self) -> PolygonMesh {
let StructuredMesh {
positions,
uv_division,
normals,
} = self;
let m = positions.len();
let n = positions[0].len();
let positions = positions.into_iter().flatten().collect();
let uv_coords = uv_division
.map(move |(udiv, vdiv)| {
udiv.into_iter()
.flat_map(|u| vdiv.iter().map(move |v| Vector2::new(u, *v)))
.collect()
})
.unwrap_or_else(Vec::new);
let normals = normals
.map(|n| n.into_iter().flatten().collect())
.unwrap_or_else(Vec::new);
let uv = !uv_coords.is_empty();
let nor = !normals.is_empty();
let quad_faces: Vec<_> = (1..m)
.flat_map(|i| (1..n).map(move |j| (i, j)))
.map(move |(i, j)| {
[
StandardVertex::tuple((i - 1) * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j - 1, uv, nor),
StandardVertex::tuple(i * n + j, uv, nor),
StandardVertex::tuple((i - 1) * n + j, uv, nor),
]
})
.collect();
let faces = Faces {
quad_faces,
..Default::default()
};
PolygonMesh {
attributes: StandardAttributes {
positions,
uv_coords,
normals,
},
faces,
}
}
}
impl<'de> Deserialize<'de> for StructuredMesh {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where D: serde::Deserializer<'de> {
#[derive(Deserialize)]
pub struct StructuredMesh_ {
positions: Vec<Vec<Point3>>,
uv_division: Option<(Vec<f64>, Vec<f64>)>,
normals: Option<Vec<Vec<Vector3>>>,
}
let StructuredMesh_ {
positions,
uv_division,
normals,
} = StructuredMesh_::deserialize(deserializer)?;
match (uv_division, normals) {
(Some(uv_division), Some(normals)) => Self::try_new(positions, uv_division, normals),
(Some(uv_division), None) => Self::try_from_positions_and_uvs(positions, uv_division),
(None, Some(normals)) => Self::try_from_positions_and_normals(positions, normals),
(None, None) => Self::try_from_positions(positions),
}
.map_err(serde::de::Error::custom)
}
sourcepub const fn new_unchecked(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
pub const fn new_unchecked(
positions: Vec<Vec<Point3>>,
uv_division: (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>
) -> StructuredMesh
Creates new structured mesh. Does not check whether the size of vectors are compatible before creation.
Examples found in repository?
120 121 122 123 124 125 126 127 128 129 130 131 132 133
pub fn try_new(
positions: Vec<Vec<Point3>>,
(u_div, v_div): (Vec<f64>, Vec<f64>),
normals: Vec<Vec<Vector3>>,
) -> Result<StructuredMesh> {
check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
check_matrix_vectors_compatibility(&normals, &u_div, &v_div)?;
check_vectors_regularity(&u_div, &v_div)?;
Ok(StructuredMesh::new_unchecked(
positions,
(u_div, v_div),
normals,
))
}
sourcepub fn positions_mut(&mut self) -> Vec<&mut [Point3]> ⓘ
pub fn positions_mut(&mut self) -> Vec<&mut [Point3]> ⓘ
Returns the vector of the mutable references to the rows of the positions matrix.
sourcepub fn uv_division(&self) -> Option<(&Vec<f64>, &Vec<f64>)>
pub fn uv_division(&self) -> Option<(&Vec<f64>, &Vec<f64>)>
Returns the divisions of uv coordinates.
sourcepub fn uv_division_mut(&mut self) -> Option<(&mut [f64], &mut [f64])>
pub fn uv_division_mut(&mut self) -> Option<(&mut [f64], &mut [f64])>
Returns the mutable slice of uv coordinates division.
sourcepub fn normals_mut(&mut self) -> Option<Vec<&mut [Vector3]>>
pub fn normals_mut(&mut self) -> Option<Vec<&mut [Vector3]>>
Returns the vector of the mutable references to the rows of the normals matrix.
sourcepub fn destruct(self) -> PolygonMesh
pub fn destruct(self) -> PolygonMesh
Creates new polygon mesh by destructing self
.
Trait Implementations§
source§impl Clone for StructuredMesh
impl Clone for StructuredMesh
source§fn clone(&self) -> StructuredMesh
fn clone(&self) -> StructuredMesh
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more