microcad_core/
render_resolution.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Render resolution
5
6use cgmath::InnerSpace;
7
8use crate::*;
9
10/// Render resolution when rendering things to polygons or meshes.
11#[derive(Debug, Clone)]
12pub struct RenderResolution {
13    /// Linear resolution in millimeters (Default = 0.1mm)
14    pub linear: Scalar,
15}
16
17impl RenderResolution {
18    /// Create new render resolution.
19    pub fn new(linear: Scalar) -> Self {
20        Self { linear }
21    }
22
23    /// Coarse render resolution of 1.0mm.
24    pub fn coarse() -> Self {
25        Self { linear: 1.0 }
26    }
27}
28
29impl std::ops::Mul<Mat3> for RenderResolution {
30    type Output = RenderResolution;
31
32    fn mul(self, rhs: Mat3) -> Self::Output {
33        let scale = (rhs.x.magnitude() * rhs.y.magnitude()).sqrt();
34        Self {
35            linear: self.linear / scale,
36        }
37    }
38}
39
40impl std::ops::Mul<Mat4> for RenderResolution {
41    type Output = RenderResolution;
42
43    fn mul(self, rhs: Mat4) -> Self::Output {
44        let scale = (rhs.x.magnitude() * rhs.y.magnitude() * rhs.z.magnitude()).powf(1.0 / 3.0);
45        Self {
46            linear: self.linear / scale,
47        }
48    }
49}
50
51impl Default for RenderResolution {
52    fn default() -> Self {
53        RenderResolution { linear: 0.1 }
54    }
55}
56
57impl std::fmt::Display for RenderResolution {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(f, "@{}mm", self.linear)
60    }
61}