+ # The Canonical View Volume, 8 vertices, and 6 faces,
+ # We consider its face normals pointing outside
+
+ v1 = NMesh.Vert(1, 1, -1)
+ v2 = NMesh.Vert(1, -1, -1)
+ v3 = NMesh.Vert(-1, -1, -1)
+ v4 = NMesh.Vert(-1, 1, -1)
+ v5 = NMesh.Vert(1, 1, 1)
+ v6 = NMesh.Vert(1, -1, 1)
+ v7 = NMesh.Vert(-1, -1, 1)
+ v8 = NMesh.Vert(-1, 1, 1)
+
+ cvv = []
+ f1 = NMesh.Face([v1, v4, v3, v2])
+ cvv.append(f1)
+ f2 = NMesh.Face([v5, v6, v7, v8])
+ cvv.append(f2)
+ f3 = NMesh.Face([v1, v2, v6, v5])
+ cvv.append(f3)
+ f4 = NMesh.Face([v2, v3, v7, v6])
+ cvv.append(f4)
+ f5 = NMesh.Face([v3, v4, v8, v7])
+ cvv.append(f5)
+ f6 = NMesh.Face([v4, v1, v5, v8])
+ cvv.append(f6)
+
+ nmesh = NMesh.GetRaw(mesh.name)
+ clippedfaces = nmesh.faces[:]
+ facelist = clippedfaces[:]
+
+ for clipface in cvv:
+
+ clippedfaces = []
+ for f in facelist:
+
+ newfaces = HSR.splitOn(clipface, f, return_positive_faces=False)
+
+ if not newfaces:
+ # Check if the face is inside the view rectangle
+ # TODO: Do this test before, it is more efficient
+ points_outside = 0
+ for v in f:
+ if abs(v[0]) > 1-EPS or abs(v[1]) > 1-EPS:
+ points_outside += 1
+
+ if points_outside != len(f):
+ clippedfaces.append(f)
+ else:
+ for nf in newfaces:
+ for v in nf:
+ nmesh.verts.append(v)
+
+ nf.mat = f.mat
+ nf.sel = f.sel
+ nf.col = [f.col[0]] * len(nf.v)
+
+ clippedfaces.append(nf)
+
+ facelist = clippedfaces[:]
+
+ nmesh.faces = facelist
+ nmesh.update()
+
+