Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaetani authored Dec 3, 2019
1 parent 41bd566 commit 920e1ba
Show file tree
Hide file tree
Showing 11 changed files with 60,700 additions and 325 deletions.
Binary file added 10/CS460_Assignment_10__Copy_.pdf
Binary file not shown.
60,127 changes: 60,127 additions & 0 deletions 10/Dragon 2.5_ply.ply

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions 10/base64.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/**
* Inspired from https://stackoverflow.com/a/36046727
*
* Modified to work directly with Float32Arrays and to match
* the Python API.
*
*/
var base64 = {};

base64.b64encode = function (array) {

array = new Uint8Array(array.buffer);

return btoa(String.fromCharCode.apply(null, array));

};

base64.b64decode = function (str) {

array = atob(str).split('').map(function (c) { return c.charCodeAt(0); });

return new Float32Array(new Uint8Array(array).buffer);

};
/**
* Inspired from https://stackoverflow.com/a/36046727
*
* Modified to work directly with Float32Arrays and to match
* the Python API.
*
*/
var base64 = {};

base64.b64encode = function (array) {

array = new Uint8Array(array.buffer);

return btoa(String.fromCharCode.apply(null, array));

};

base64.b64decode = function (str) {

array = atob(str).split('').map(function (c) { return c.charCodeAt(0); });

return new Float32Array(new Uint8Array(array).buffer);

};
1 change: 1 addition & 0 deletions 10/column.gltf

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions 10/dragon.gltf

Large diffs are not rendered by default.

269 changes: 163 additions & 106 deletions 10/gltf.py
Original file line number Diff line number Diff line change
@@ -1,106 +1,163 @@
import numpy as np
import base64

VERTICES = np.array([0.,0.,0., 0.,1.,0., 1.,0.,0.], dtype=np.float32)
INDICES = np.array([0, 1, 2], dtype=np.ushort)

HOWMANY = 3
MAX_X = 1
MAX_Y = 1
MAX_Z = 0
MIN_X = 0
MIN_Y = 0
MIN_Z = 0
MAX = 2
MIN = 0

HOWMANYBYTES_V = VERTICES.nbytes
HOWMANYBYTES_I = INDICES.nbytes

B64_VERTICES = base64.b64encode(VERTICES)
B64_INDICES = base64.b64encode(INDICES)

gltf = {
"asset": {
"version": "2.0",
"generator": "CS460 Magic Fingers"
},

"accessors": [
{
"bufferView": 0,
"byteOffset": 0,
"componentType": 5126,
"count": HOWMANY,
"type": "VEC3",
"max": [MAX_X, MAX_Y, MAX_Z],
"min": [MIN_X, MIN_Y, MIN_Z]
},
{
"bufferView": 1,
"byteOffset": 0,
"componentType": 5123,
"count": HOWMANY,
"type": "SCALAR",
"max": [MAX],
"min": [MIN]
}
],

"bufferViews": [
{
"buffer": 0,
"byteOffset": 0,
"byteLength": HOWMANYBYTES_V,
"target": 34962
},
{
"buffer": 1,
"byteOffset": 0,
"byteLength": HOWMANYBYTES_I,
"target": 34963
}
],

"buffers": [
{
"uri": "data:application/octet-stream;base64,"+str(B64_VERTICES, 'utf-8'),
"byteLength": HOWMANYBYTES_V
},
{
"uri": "data:application/octet-stream;base64,"+str(B64_INDICES, 'utf-8'),
"byteLength": HOWMANYBYTES_I
}
],

"meshes": [
{
"primitives": [{
"mode": 4,
"attributes": {
"POSITION": 0
},
"indices": 1
}]
}
],

"nodes": [
{
"mesh": 0
}
],

"scenes": [
{
"nodes": [
0
]
}
],

"scene": 0
}

print ( str(gltf).replace("'", '"') ) # we need double quotes instead of single quotes

import numpy as np
import base64

with open("Dragon 2.5_ply.ply", 'r') as f:
lines = f.readlines()

counter = 0
vertices_coming = False
faces_coming = False

## get the infomation of vertices and faces
for i,l in enumerate(lines):

if l.startswith('element vertex'):
vertexcounter = int(l.split(' ')[-1].strip())
elif l.startswith('element face'):
facecounter = int(l.split(' ')[-1].strip())
elif l.startswith('end_header'):
break

vertex_data = lines[i+1:i+vertexcounter+1]
face_data = lines[i+vertexcounter+1:]

## create the VERTICES array
VERTICES = []
MAX_X = -np.inf
MAX_Y = -np.inf
MAX_Z = -np.inf
MIN_X = np.inf
MIN_Y = np.inf
MIN_Z = np.inf

## parse all vertices and find the required fields
for v in vertex_data:
v = [float(v.strip()) for v in v.strip().split(' ')]

MAX_X = max(MAX_X, v[0])
MAX_Y = max(MAX_Y, v[1])
MAX_Z = max(MAX_Z, v[2])
MIN_X = min(MIN_X, v[0])
MIN_Y = min(MIN_Y, v[1])
MIN_Z = min(MIN_Z, v[2])

VERTICES += v

## parse all indices and find the required fields
INDICES = []
MAX = -np.inf
MIN = np.inf
for i in face_data:
i = [int(i) for i in i.strip().split(' ')[1:]]
INDICES += i

MAX = max(MAX, i[0])
MAX = max(MAX, i[1])
MAX = max(MAX, i[2])
MIN = min(MIN, i[0])
MIN = min(MIN, i[1])
MIN = min(MIN, i[2])

VERTICES = np.array(VERTICES, dtype=np.float32)
INDICES = np.array(INDICES, dtype=np.ushort)


HOWMANY_V = len(VERTICES) / 3 # because its the number of VEC3 groups
HOWMANY_I = len(INDICES)

HOWMANYBYTES_V = VERTICES.nbytes
HOWMANYBYTES_I = INDICES.nbytes

## base 64 code
B64_VERTICES = base64.b64encode(VERTICES)
B64_INDICES = base64.b64encode(INDICES)

## generate the glTF JSON code
gltf = {
"asset": {
"version": "2.0",
"generator": "CS460 Magic Fingers"
},

"accessors": [
{
"bufferView": 0,
"byteOffset": 0,
"componentType": 5126,
"count": HOWMANY_V,
"type": "VEC3",
"max": [MAX_X, MAX_Y, MAX_Z],
"min": [MIN_X, MIN_Y, MIN_Z]
},
{
"bufferView": 1,
"byteOffset": 0,
"componentType": 5123,
"count": HOWMANY_I,
"type": "SCALAR",
"max": [MAX],
"min": [MIN]
}
],

"bufferViews": [
{
"buffer": 0,
"byteOffset": 0,
"byteLength": HOWMANYBYTES_V,
"target": 34962
},
{
"buffer": 1,
"byteOffset": 0,
"byteLength": HOWMANYBYTES_I,
"target": 34963
}
],

"buffers": [
{
"uri": "data:application/octet-stream;base64,"+str(B64_VERTICES),
"byteLength": HOWMANYBYTES_V
},
{
"uri": "data:application/octet-stream;base64,"+str(B64_INDICES),
"byteLength": HOWMANYBYTES_I
}
],

"meshes": [
{
"primitives": [{
"mode": 4,
"attributes": {
"POSITION": 0
},
"indices": 1
}]
}
],

"nodes": [
{
"mesh": 0
}
],

"scenes": [
{
"nodes": [
0
]
}
],

"scene": 0
}


print ( str(gltf).replace("'", '"') ) # we need double quotes instead of single quotes

text_file = open("dragon.gltf", "w")
n = text_file.write(str(gltf).replace("'", '"'))
text_file.close()
Binary file added 10/golden_column.ply
Binary file not shown.
Binary file added 10/golden_column_0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 920e1ba

Please sign in to comment.