forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (124 loc) · 4.23 KB
/
index.js
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
/*
JSCAD Object to OBJ Format Serialization
## License
Copyright (c) 2021 JSCAD Organization https://github.com/jscad
All code released under MIT license
Notes:
1) geom2 conversion to:
none
2) geom3 conversion to:
mesh
3) path2 conversion to:
none
*/
/**
* Serializer of JSCAD geometries to OBJ source data
*
* The serialization of the following geometries are possible.
* - serialization of 3D geometry (geom3) to OBJ object (a unique mesh containing both vertices and volumes)
*
* @module io/obj-serializer
* @example
* const { serializer, mimeType } = require('@jscad/obj-serializer')
*/
const { colors, geometries, modifiers } = require('@jscad/modeling')
const { flatten, toArray } = require('@jscad/array-utils')
const mimeType = 'application/object'
/**
* Serialize the give objects (geometry) to OBJ source data.
* @param {Object} options - options for serialization
* @param {Boolean} [options.triangulate=true] - triangle or polygon faces
* @param {Function} [options.statusCallback] - call back function for progress ({ progress: 0-100 })
* @param {...Object} objects - objects to serialize into OBJ source data
* @returns {Array} serialized contents, OBJ source data
* @alias module:io/obj-serializer.serialize
* @example
* const geometry = primitives.cube()
* const objData = serializer({}, geometry)
*/
const serialize = (options, ...objects) => {
const defaults = {
statusCallback: null,
triangulate: true // OBJ file supports polygon faces, but triangulate by default for safety
}
options = Object.assign({}, defaults, options)
objects = flatten(objects)
// convert only 3D geometries
let objects3d = objects.filter((object) => geometries.geom3.isA(object))
if (objects3d.length === 0) throw new Error('only 3D geometries can be serialized to OBJ')
if (objects.length !== objects3d.length) console.warn('some objects could not be serialized to OBJ')
// snap to grid and convert to triangles
objects3d = toArray(modifiers.generalize({ snap: true, triangulate: options.triangulate }, objects3d))
options.statusCallback && options.statusCallback({ progress: 0 })
// construct the contents of the OBJ file
let body = '# Wavefront OBJ file generated by JSCAD\n'
// find unique vertices
const vertices = []
// convert objects
// TODO: group objects together
let previousColor = 'default'
objects3d.forEach((object, i) => {
options.statusCallback && options.statusCallback({ progress: 100 * i / objects3d.length })
body += '\n'
const objectColor = getColorName(object)
const polygons = geometries.geom3.toPolygons(object)
.filter((p) => p.vertices.length >= 3)
polygons.forEach((polygon) => {
polygon.vertices.forEach((vertex) => {
const vertexString = convertVertex(vertex)
if (vertices.indexOf(vertexString) < 0) {
// add unique vertices
vertices.push(vertexString)
body += `${vertexString}\n`
}
})
})
body += '\n'
// convert faces
polygons.forEach((polygon) => {
// convert vertices to indices
const indices = polygon.vertices
.map((v) => vertices.indexOf(convertVertex(v)) + 1)
// set face color
const color = getColorName(polygon) || objectColor || 'default'
if (color !== previousColor) {
body += `usemtl ${color}\n`
previousColor = color
}
body += `f ${indices.join(' ')}\n`
})
})
options.statusCallback && options.statusCallback({ progress: 100 })
return [body]
}
/**
* Convert a vertex to an obj "v" string
*/
const convertVertex = (vertex) => `v ${vertex[0]} ${vertex[1]} ${vertex[2]}`
/**
* Get the closest css color name
*/
const getColorName = (object) => {
let colorName
if (object.color) {
const r = object.color[0]
const g = object.color[1]
const b = object.color[2]
// find the closest css color
let closest = 255 + 255 + 255
for (const name in colors.cssColors) {
const rgb = colors.cssColors[name]
const diff = Math.abs(r - rgb[0]) + Math.abs(g - rgb[1]) + Math.abs(b - rgb[2])
if (diff < closest) {
colorName = name
if (diff === 0) break
closest = diff
}
}
}
return colorName
}
module.exports = {
serialize,
mimeType
}