forked from drewmcneely/photometry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopex.py
100 lines (76 loc) · 3.24 KB
/
topex.py
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
'''
photometry.py. Visualize the photometric output of a Wavefront obj. model.
Copyright (C) 2020 Drew Allen McNeely
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
'''
import numpy as np
from pathlib import Path
import pymesh as pm
from operator import itemgetter
from materials import Facet, Model
from geometry import SpherePoint
from visualization import plot_function_triangles as plot
from itertools import islice, repeat
class Topex:
def __init__(self, mesh):
attributes = [
"face_area",
"face_normal"
]
for a in attributes:
mesh.add_attribute(a)
areas = mesh.get_attribute("face_area")
normals = mesh.get_attribute("face_normal")
length_to_split = repeat(3, len(areas))
Inputt = iter(normals)
normals = [list(islice(Inputt, elem)) for elem in length_to_split]
normal_directions = [SpherePoint.from_list(n) for n in normals]
facets = [Facet(area=a, normal_direction=n) for a,n in zip(areas, normal_directions)]
model = Model(facets)
self.mesh = mesh
self.areas = areas
self.normals = normal_directions
self.facet_model = model
@classmethod
def from_path(cls, path):
mesh = pm.load_mesh(str(topex_file_path))
return cls(mesh)
@property
def total_area(self): return sum(self.areas)
def components(self):
comps = pm.separate_mesh(self.mesh)
return [Topex(c) for c in comps]
def reduced(self, n=10):
components = self.components()
areas = [c.total_area for c in components]
area_tuples = zip(components, areas)
components = sorted(area_tuples, key=itemgetter(1), reverse=True)
components = components[:10]
components = [c[0].mesh for c in components]
mesh = pm.merge_meshes(components)
return Topex(mesh)
def scatter(self, light_direction, viewer_direction):
return self.facet_model.scatter(light_direction, viewer_direction)
def total_scatter(self, viewer_direction):
return self.facet_model.total_scatter(viewer_direction)
if __name__ == "__main__":
#topex_dir = Path('/home/drew/dev/photometry/data/models/topex-poseidon/obj/')
topex_dir = Path('/home/drew/dev/photometry/photometry/')
#topex_file_path = topex_dir / "Topex-Posidon-composite.obj"
topex_file_path = topex_dir / "cube.obj"
topex = Topex.from_path(topex_file_path)
#topex = topex.reduced()
#light_direction = SpherePoint.from_list([1,1,1])
def func(viewer_direction):
return topex.total_scatter(viewer_direction)
plot(func)