VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
primitives.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 25.07.2025.
5//
6
7#include "primitives.h"
8
9namespace visutwin::canvas
10{
11 void Frustum::create(const Matrix4& viewProjection)
12 {
13 // We are interested in columns of the matrix, so transpose because we can access only rows:
14 const Matrix4 mat = viewProjection.transpose();
15
16 const auto c0 = mat.getColumn(0);
17 const auto c1 = mat.getColumn(1);
18 const auto c2 = mat.getColumn(2);
19 const auto c3 = mat.getColumn(3);
20
21 // Near plane (OpenGL-style clip space: z >= -w):
22 planes[0] = (c3 + c2).planeNormalize();
23
24 // Far plane:
25 planes[1] = (c3 - c2).planeNormalize();
26
27 // Left plane:
28 planes[2] = (c3 + c0).planeNormalize();
29
30 // Right plane:
31 planes[3] = (c3 - c0).planeNormalize();
32
33 // Top plane:
34 planes[4] = (c3 - c1).planeNormalize();
35
36 // Bottom plane:
37 planes[5] = (c3 + c1).planeNormalize();
38 }
39
40 bool Frustum::checkPoint(const Vector3& point) const
41 {
42 const auto p = Vector4(point);
43 for (const auto& plane : planes)
44 {
45 if (plane.planeDotCoord(p) < 0.0f)
46 {
47 return false;
48 }
49 }
50 return true;
51 }
52
53 bool Frustum::checkSphere(const Vector3& center, const float radius) const
54 {
55 const auto c = Vector4(center);
56 for (const auto& plane : planes)
57 {
58 if (plane.planeDotCoord(c) < -radius)
59 {
60 return false;
61 }
62 }
63 return true;
64 }
65}
bool checkPoint(const Vector3 &) const
bool checkSphere(const Vector3 &, float) const
void create(const Matrix4 &viewProjection)
4x4 column-major transformation matrix with SIMD acceleration.
Definition matrix4.h:31
Matrix4 transpose() const
Definition matrix4.h:170
Vector4 getColumn(int col) const
3D vector for positions, directions, and normals with multi-backend SIMD acceleration.
Definition vector3.h:29
4D vector for homogeneous coordinates, color values, and SIMD operations.
Definition vector4.h:20