acadrust 0.3.3

A pure Rust library for reading and writing CAD files in DXF format (ASCII and Binary) and DWG format (Binary).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
//! Viewport entity - Paper space viewport for model space views

use crate::entities::{Entity, EntityCommon};
use crate::types::{BoundingBox3D, Color, Handle, LineWeight, Transparency, Vector3};

/// Viewport status flags
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ViewportStatusFlags {
    /// Viewport is on (visible)
    pub is_on: bool,
    /// Perspective mode active
    pub perspective: bool,
    /// Front clipping on
    pub front_clipping: bool,
    /// Back clipping on
    pub back_clipping: bool,
    /// UCS follow mode
    pub ucs_follow: bool,
    /// Front clip not at eye
    pub front_clip_not_at_eye: bool,
    /// UCS icon visibility
    pub ucs_icon_visible: bool,
    /// UCS icon at origin
    pub ucs_icon_at_origin: bool,
    /// Fast zoom enabled
    pub fast_zoom: bool,
    /// Snap mode on
    pub snap_on: bool,
    /// Grid mode on
    pub grid_on: bool,
    /// Isometric snap style
    pub isometric_snap: bool,
    /// Hide plot
    pub hide_plot: bool,
    /// kIsoPairTop
    pub iso_pair_top: bool,
    /// kIsoPairRight
    pub iso_pair_right: bool,
    /// Viewport locked
    pub locked: bool,
}

impl ViewportStatusFlags {
    /// Create default viewport status (on, visible)
    pub fn default_on() -> Self {
        Self {
            is_on: true,
            ..Default::default()
        }
    }

    /// Create from DXF bit flag value
    pub fn from_bits(bits: i32) -> Self {
        Self {
            is_on: (bits & (1 << 0)) != 0,
            perspective: (bits & (1 << 1)) != 0,
            front_clipping: (bits & (1 << 2)) != 0,
            back_clipping: (bits & (1 << 3)) != 0,
            ucs_follow: (bits & (1 << 4)) != 0,
            front_clip_not_at_eye: (bits & (1 << 5)) != 0,
            ucs_icon_visible: (bits & (1 << 6)) != 0,
            ucs_icon_at_origin: (bits & (1 << 7)) != 0,
            fast_zoom: (bits & (1 << 8)) != 0,
            snap_on: (bits & (1 << 9)) != 0,
            grid_on: (bits & (1 << 10)) != 0,
            isometric_snap: (bits & (1 << 11)) != 0,
            hide_plot: (bits & (1 << 12)) != 0,
            iso_pair_top: (bits & (1 << 13)) != 0,
            iso_pair_right: (bits & (1 << 14)) != 0,
            locked: (bits & (1 << 15)) != 0,
        }
    }

    /// Convert to DXF bit flag value
    pub fn to_bits(&self) -> i32 {
        let mut bits = 0;
        if self.is_on { bits |= 1 << 0; }
        if self.perspective { bits |= 1 << 1; }
        if self.front_clipping { bits |= 1 << 2; }
        if self.back_clipping { bits |= 1 << 3; }
        if self.ucs_follow { bits |= 1 << 4; }
        if self.front_clip_not_at_eye { bits |= 1 << 5; }
        if self.ucs_icon_visible { bits |= 1 << 6; }
        if self.ucs_icon_at_origin { bits |= 1 << 7; }
        if self.fast_zoom { bits |= 1 << 8; }
        if self.snap_on { bits |= 1 << 9; }
        if self.grid_on { bits |= 1 << 10; }
        if self.isometric_snap { bits |= 1 << 11; }
        if self.hide_plot { bits |= 1 << 12; }
        if self.iso_pair_top { bits |= 1 << 13; }
        if self.iso_pair_right { bits |= 1 << 14; }
        if self.locked { bits |= 1 << 15; }
        bits
    }
}

/// Render mode for viewport display
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ViewportRenderMode {
    /// 2D Wireframe
    #[default]
    Wireframe2D = 0,
    /// 3D Wireframe
    Wireframe3D = 1,
    /// Hidden line removal
    HiddenLine = 2,
    /// Flat shaded
    FlatShaded = 3,
    /// Gouraud shaded
    GouraudShaded = 4,
    /// Flat shaded with edges
    FlatShadedWithEdges = 5,
    /// Gouraud shaded with edges
    GouraudShadedWithEdges = 6,
}

impl ViewportRenderMode {
    /// Create from DXF value
    pub fn from_value(value: i16) -> Self {
        match value {
            1 => ViewportRenderMode::Wireframe3D,
            2 => ViewportRenderMode::HiddenLine,
            3 => ViewportRenderMode::FlatShaded,
            4 => ViewportRenderMode::GouraudShaded,
            5 => ViewportRenderMode::FlatShadedWithEdges,
            6 => ViewportRenderMode::GouraudShadedWithEdges,
            _ => ViewportRenderMode::Wireframe2D,
        }
    }

    /// Convert to DXF value
    pub fn to_value(&self) -> i16 {
        *self as i16
    }
}

/// Grid display flags
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GridFlags {
    /// Grid beyond limits
    pub beyond_limits: bool,
    /// Adaptive grid
    pub adaptive: bool,
    /// Allow subdivision
    pub subdivision: bool,
    /// Follow dynamic UCS
    pub follow_dynamic: bool,
}

impl GridFlags {
    /// Create from bits
    pub fn from_bits(bits: i16) -> Self {
        Self {
            beyond_limits: (bits & 1) != 0,
            adaptive: (bits & 2) != 0,
            subdivision: (bits & 4) != 0,
            follow_dynamic: (bits & 8) != 0,
        }
    }

    /// Convert to bits
    pub fn to_bits(&self) -> i16 {
        let mut bits = 0;
        if self.beyond_limits { bits |= 1; }
        if self.adaptive { bits |= 2; }
        if self.subdivision { bits |= 4; }
        if self.follow_dynamic { bits |= 8; }
        bits
    }
}

/// Viewport entity - defines a view of model space within paper space
///
/// Viewports are used in paper space layouts to create windows into model space.
/// They define the position, size, and viewing parameters for displaying
/// model space content.
///
/// # DXF Entity Type
/// VIEWPORT
///
/// # Example
/// ```ignore
/// use acadrust::entities::Viewport;
/// use acadrust::types::Vector3;
///
/// let mut viewport = Viewport::new();
/// viewport.center = Vector3::new(5.0, 5.0, 0.0);
/// viewport.width = 10.0;
/// viewport.height = 8.0;
/// viewport.view_target = Vector3::new(0.0, 0.0, 0.0);
/// viewport.view_direction = Vector3::new(0.0, 0.0, 1.0);
/// viewport.custom_scale = 0.5; // 1:2 scale
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Viewport {
    /// Common entity properties
    pub common: EntityCommon,
    /// Center point in paper space
    pub center: Vector3,
    /// Width in paper space units
    pub width: f64,
    /// Height in paper space units
    pub height: f64,
    /// Viewport status flags
    pub status: ViewportStatusFlags,
    /// Viewport ID (unique within the drawing)
    pub id: i16,
    /// View center point (DCS - Display Coordinate System)
    pub view_center: Vector3,
    /// Snap base point
    pub snap_base: Vector3,
    /// Snap spacing
    pub snap_spacing: Vector3,
    /// Grid spacing
    pub grid_spacing: Vector3,
    /// View direction vector (WCS)
    pub view_direction: Vector3,
    /// View target point (WCS)
    pub view_target: Vector3,
    /// Lens length (mm)
    pub lens_length: f64,
    /// Front clipping plane Z value
    pub front_clip_z: f64,
    /// Back clipping plane Z value
    pub back_clip_z: f64,
    /// View height (in model space units)
    pub view_height: f64,
    /// Snap angle
    pub snap_angle: f64,
    /// View twist angle
    pub twist_angle: f64,
    /// Circle zoom percent (1-20000)
    pub circle_sides: i16,
    /// Frozen layer handles
    pub frozen_layers: Vec<Handle>,
    /// Render mode
    pub render_mode: ViewportRenderMode,
    /// UCS per viewport flag
    pub ucs_per_viewport: bool,
    /// UCS icon visibility
    pub ucs_icon_visible: bool,
    /// UCS origin
    pub ucs_origin: Vector3,
    /// UCS X axis
    pub ucs_x_axis: Vector3,
    /// UCS Y axis
    pub ucs_y_axis: Vector3,
    /// UCS handle
    pub ucs_handle: Handle,
    /// Base UCS handle
    pub base_ucs_handle: Handle,
    /// UCS orthographic type
    pub ucs_ortho_type: i16,
    /// Elevation
    pub elevation: f64,
    /// Shade plot mode
    pub shade_plot_mode: i16,
    /// Grid flags
    pub grid_flags: GridFlags,
    /// Grid major frequency
    pub grid_major: i16,
    /// Background handle
    pub background_handle: Handle,
    /// Shade plot handle
    pub shade_plot_handle: Handle,
    /// Visual style handle
    pub visual_style_handle: Handle,
    /// Default lighting on
    pub default_lighting: bool,
    /// Default lighting type
    pub default_lighting_type: i16,
    /// View brightness
    pub brightness: f64,
    /// View contrast
    pub contrast: f64,
    /// Ambient light color (RGB)
    pub ambient_color: i32,
    /// Custom scale factor
    pub custom_scale: f64,
}

impl Viewport {
    /// Create a new viewport with default settings
    pub fn new() -> Self {
        Self {
            common: EntityCommon::default(),
            center: Vector3::ZERO,
            width: 297.0,   // A4 width in mm
            height: 210.0,  // A4 height in mm
            status: ViewportStatusFlags::default_on(),
            id: 0,
            view_center: Vector3::ZERO,
            snap_base: Vector3::ZERO,
            snap_spacing: Vector3::new(10.0, 10.0, 0.0),
            grid_spacing: Vector3::new(10.0, 10.0, 0.0),
            view_direction: Vector3::UNIT_Z,
            view_target: Vector3::ZERO,
            lens_length: 50.0,
            front_clip_z: 0.0,
            back_clip_z: 0.0,
            view_height: 210.0,
            snap_angle: 0.0,
            twist_angle: 0.0,
            circle_sides: 1000,
            frozen_layers: Vec::new(),
            render_mode: ViewportRenderMode::Wireframe2D,
            ucs_per_viewport: false,
            ucs_icon_visible: true,
            ucs_origin: Vector3::ZERO,
            ucs_x_axis: Vector3::UNIT_X,
            ucs_y_axis: Vector3::UNIT_Y,
            ucs_handle: Handle::NULL,
            base_ucs_handle: Handle::NULL,
            ucs_ortho_type: 0,
            elevation: 0.0,
            shade_plot_mode: 0,
            grid_flags: GridFlags::default(),
            grid_major: 5,
            background_handle: Handle::NULL,
            shade_plot_handle: Handle::NULL,
            visual_style_handle: Handle::NULL,
            default_lighting: true,
            default_lighting_type: 1,
            brightness: 0.0,
            contrast: 0.0,
            ambient_color: 0,
            custom_scale: 1.0,
        }
    }

    /// Create a viewport with specific size and position
    pub fn with_size(center: Vector3, width: f64, height: f64) -> Self {
        let mut vp = Self::new();
        vp.center = center;
        vp.width = width;
        vp.height = height;
        vp
    }

    /// Create a viewport for model space (ID 1)
    pub fn model_space() -> Self {
        let mut vp = Self::new();
        vp.id = 1;
        vp
    }

    /// Set the view target (what the viewport is looking at)
    pub fn set_view_target(&mut self, target: Vector3) {
        self.view_target = target;
    }

    /// Set the view direction
    pub fn set_view_direction(&mut self, direction: Vector3) {
        let normalized = direction.normalize();
        if normalized.length_squared() > 0.0 {
            self.view_direction = normalized;
        } else {
            self.view_direction = Vector3::UNIT_Z;
        }
    }

    /// Set the view height (zoom level in model space)
    pub fn set_view_height(&mut self, height: f64) {
        self.view_height = height;
    }

    /// Calculate the scale factor (paper space / model space)
    pub fn scale(&self) -> f64 {
        if self.view_height.abs() > 1e-10 {
            self.height / self.view_height
        } else {
            1.0
        }
    }

    /// Set the scale factor
    pub fn set_scale(&mut self, scale: f64) {
        if scale.abs() > 1e-10 {
            self.view_height = self.height / scale;
            self.custom_scale = scale;
        }
    }

    /// Lock the viewport (prevent zoom/pan)
    pub fn lock(&mut self) {
        self.status.locked = true;
    }

    /// Unlock the viewport
    pub fn unlock(&mut self) {
        self.status.locked = false;
    }

    /// Check if viewport is locked
    pub fn is_locked(&self) -> bool {
        self.status.locked
    }

    /// Turn viewport on
    pub fn turn_on(&mut self) {
        self.status.is_on = true;
    }

    /// Turn viewport off
    pub fn turn_off(&mut self) {
        self.status.is_on = false;
    }

    /// Check if viewport is on
    pub fn is_on(&self) -> bool {
        self.status.is_on
    }

    /// Add a frozen layer by handle
    pub fn freeze_layer(&mut self, layer_handle: Handle) {
        if !self.frozen_layers.contains(&layer_handle) {
            self.frozen_layers.push(layer_handle);
        }
    }

    /// Remove a frozen layer
    pub fn thaw_layer(&mut self, layer_handle: Handle) {
        self.frozen_layers.retain(|h| *h != layer_handle);
    }

    /// Set the render mode
    pub fn set_render_mode(&mut self, mode: ViewportRenderMode) {
        self.render_mode = mode;
    }

    /// Get the paper space bounds of this viewport
    pub fn paper_bounds(&self) -> BoundingBox3D {
        let half_w = self.width / 2.0;
        let half_h = self.height / 2.0;
        BoundingBox3D::new(
            Vector3::new(self.center.x - half_w, self.center.y - half_h, 0.0),
            Vector3::new(self.center.x + half_w, self.center.y + half_h, 0.0),
        )
    }

    /// Set a standard view (top, front, right, isometric, etc.)
    pub fn set_standard_view(&mut self, view: StandardView) {
        match view {
            StandardView::Top => {
                self.view_direction = Vector3::UNIT_Z;
                self.twist_angle = 0.0;
            }
            StandardView::Bottom => {
                self.view_direction = -Vector3::UNIT_Z;
                self.twist_angle = 0.0;
            }
            StandardView::Front => {
                self.view_direction = -Vector3::UNIT_Y;
                self.twist_angle = 0.0;
            }
            StandardView::Back => {
                self.view_direction = Vector3::UNIT_Y;
                self.twist_angle = 0.0;
            }
            StandardView::Left => {
                self.view_direction = -Vector3::UNIT_X;
                self.twist_angle = 0.0;
            }
            StandardView::Right => {
                self.view_direction = Vector3::UNIT_X;
                self.twist_angle = 0.0;
            }
            StandardView::SWIsometric => {
                self.view_direction = Vector3::new(-1.0, -1.0, 1.0).normalize();
                self.twist_angle = 0.0;
            }
            StandardView::SEIsometric => {
                self.view_direction = Vector3::new(1.0, -1.0, 1.0).normalize();
                self.twist_angle = 0.0;
            }
            StandardView::NEIsometric => {
                self.view_direction = Vector3::new(1.0, 1.0, 1.0).normalize();
                self.twist_angle = 0.0;
            }
            StandardView::NWIsometric => {
                self.view_direction = Vector3::new(-1.0, 1.0, 1.0).normalize();
                self.twist_angle = 0.0;
            }
        }
    }

    /// Builder: Set center
    pub fn with_center(mut self, center: Vector3) -> Self {
        self.center = center;
        self
    }

    /// Builder: Set view target
    pub fn with_view_target(mut self, target: Vector3) -> Self {
        self.view_target = target;
        self
    }

    /// Builder: Set view direction
    pub fn with_view_direction(mut self, direction: Vector3) -> Self {
        let normalized = direction.normalize();
        if normalized.length_squared() > 0.0 {
            self.view_direction = normalized;
        } else {
            self.view_direction = Vector3::UNIT_Z;
        }
        self
    }

    /// Builder: Set scale
    pub fn with_scale(mut self, scale: f64) -> Self {
        self.set_scale(scale);
        self
    }

    /// Builder: Set locked
    pub fn with_locked(mut self) -> Self {
        self.status.locked = true;
        self
    }
}

/// Standard view directions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum StandardView {
    /// Top view (looking down Z)
    Top,
    /// Bottom view (looking up Z)
    Bottom,
    /// Front view (looking into -Y)
    Front,
    /// Back view (looking into +Y)
    Back,
    /// Left view (looking into -X)
    Left,
    /// Right view (looking into +X)
    Right,
    /// SW Isometric
    SWIsometric,
    /// SE Isometric
    SEIsometric,
    /// NE Isometric
    NEIsometric,
    /// NW Isometric
    NWIsometric,
}

impl Default for Viewport {
    fn default() -> Self {
        Self::new()
    }
}

impl Entity for Viewport {
    fn handle(&self) -> Handle {
        self.common.handle
    }

    fn set_handle(&mut self, handle: Handle) {
        self.common.handle = handle;
    }

    fn layer(&self) -> &str {
        &self.common.layer
    }

    fn set_layer(&mut self, layer: String) {
        self.common.layer = layer;
    }

    fn color(&self) -> Color {
        self.common.color
    }

    fn set_color(&mut self, color: Color) {
        self.common.color = color;
    }

    fn line_weight(&self) -> LineWeight {
        self.common.line_weight
    }

    fn set_line_weight(&mut self, weight: LineWeight) {
        self.common.line_weight = weight;
    }

    fn transparency(&self) -> Transparency {
        self.common.transparency
    }

    fn set_transparency(&mut self, transparency: Transparency) {
        self.common.transparency = transparency;
    }

    fn is_invisible(&self) -> bool {
        self.common.invisible
    }

    fn set_invisible(&mut self, invisible: bool) {
        self.common.invisible = invisible;
    }

    fn bounding_box(&self) -> BoundingBox3D {
        self.paper_bounds()
    }

    fn translate(&mut self, offset: Vector3) {
        super::translate::translate_viewport(self, offset);
    }

    fn entity_type(&self) -> &'static str {
        "VIEWPORT"
    }
    
    fn apply_transform(&mut self, transform: &crate::types::Transform) {
        super::transform::transform_viewport(self, transform);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_viewport_creation() {
        let vp = Viewport::new();
        assert!(vp.is_on());
        assert!(!vp.is_locked());
        assert_eq!(vp.view_direction, Vector3::UNIT_Z);
    }

    #[test]
    fn test_viewport_with_size() {
        let vp = Viewport::with_size(
            Vector3::new(100.0, 100.0, 0.0),
            200.0,
            150.0,
        );
        assert_eq!(vp.center, Vector3::new(100.0, 100.0, 0.0));
        assert_eq!(vp.width, 200.0);
        assert_eq!(vp.height, 150.0);
    }

    #[test]
    fn test_viewport_scale() {
        let mut vp = Viewport::new();
        vp.height = 100.0;
        vp.view_height = 200.0;
        
        assert!((vp.scale() - 0.5).abs() < 1e-10);
        
        vp.set_scale(0.25);
        assert!((vp.view_height - 400.0).abs() < 1e-10);
    }

    #[test]
    fn test_viewport_lock() {
        let mut vp = Viewport::new();
        assert!(!vp.is_locked());
        
        vp.lock();
        assert!(vp.is_locked());
        
        vp.unlock();
        assert!(!vp.is_locked());
    }

    #[test]
    fn test_viewport_frozen_layers() {
        let mut vp = Viewport::new();
        let layer1 = Handle::new(100);
        let layer2 = Handle::new(200);
        
        vp.freeze_layer(layer1);
        vp.freeze_layer(layer2);
        assert_eq!(vp.frozen_layers.len(), 2);
        
        vp.thaw_layer(layer1);
        assert_eq!(vp.frozen_layers.len(), 1);
        assert!(vp.frozen_layers.contains(&layer2));
    }

    #[test]
    fn test_viewport_standard_views() {
        let mut vp = Viewport::new();
        
        vp.set_standard_view(StandardView::Front);
        assert_eq!(vp.view_direction, -Vector3::UNIT_Y);
        
        vp.set_standard_view(StandardView::Top);
        assert_eq!(vp.view_direction, Vector3::UNIT_Z);
        
        vp.set_standard_view(StandardView::NEIsometric);
        // Check it's roughly (1, 1, 1) normalized
        let expected = Vector3::new(1.0, 1.0, 1.0).normalize();
        assert!((vp.view_direction.x - expected.x).abs() < 1e-10);
        assert!((vp.view_direction.y - expected.y).abs() < 1e-10);
        assert!((vp.view_direction.z - expected.z).abs() < 1e-10);
    }

    #[test]
    fn test_viewport_paper_bounds() {
        let vp = Viewport::with_size(
            Vector3::new(100.0, 100.0, 0.0),
            200.0,
            100.0,
        );
        
        let bounds = vp.paper_bounds();
        assert_eq!(bounds.min.x, 0.0);
        assert_eq!(bounds.min.y, 50.0);
        assert_eq!(bounds.max.x, 200.0);
        assert_eq!(bounds.max.y, 150.0);
    }

    #[test]
    fn test_viewport_status_flags() {
        let flags = ViewportStatusFlags::from_bits(0b1000000000000001);
        assert!(flags.is_on);
        assert!(flags.locked);
        assert!(!flags.perspective);
        
        assert_eq!(flags.to_bits(), 0b1000000000000001);
    }

    #[test]
    fn test_viewport_render_mode() {
        let mut vp = Viewport::new();
        vp.set_render_mode(ViewportRenderMode::HiddenLine);
        assert_eq!(vp.render_mode, ViewportRenderMode::HiddenLine);
    }

    #[test]
    fn test_viewport_translate() {
        let mut vp = Viewport::with_size(
            Vector3::new(0.0, 0.0, 0.0),
            100.0,
            100.0,
        );
        
        vp.translate(Vector3::new(50.0, 50.0, 0.0));
        assert_eq!(vp.center, Vector3::new(50.0, 50.0, 0.0));
    }

    #[test]
    fn test_viewport_builder() {
        let vp = Viewport::new()
            .with_center(Vector3::new(100.0, 100.0, 0.0))
            .with_view_target(Vector3::new(50.0, 50.0, 0.0))
            .with_scale(0.5)
            .with_locked();
        
        assert_eq!(vp.center, Vector3::new(100.0, 100.0, 0.0));
        assert_eq!(vp.view_target, Vector3::new(50.0, 50.0, 0.0));
        assert!(vp.is_locked());
    }
}