Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Federico'
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Ramirez committed Apr 21, 2021
2 parents 5e562e2 + 4acba04 commit a2bc274
Show file tree
Hide file tree
Showing 7 changed files with 485 additions and 187 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
11 changes: 2 additions & 9 deletions code/GraphicElements.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function GraphicElements() {
function GraphicElements(nodectx) {
this.sketch = new JitterObject("jit.gl.sketch");
this.sketch.drawto = nodectx;
this.sketch.depth_enable = 0;
this.sketch.layer = FRONT;
this.sketch.color = RED;
Expand All @@ -11,14 +12,6 @@ function GraphicElements() {
return this.sketch.screentoworld(screenCoord);
}

this.setDrawto = function(drawto) {
this.sketch.drawto = drawto;
}

this.initGraphicElements = function() {
this.setDrawto(nodeCTX.name);
}

this.drawCircle = function(coordsWorld) {
this.sketch.reset();
this.sketch.moveto(coordsWorld);
Expand Down
173 changes: 110 additions & 63 deletions code/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function Mesh() {
this.nurbs = null;
this.nurbsDim = [40, 40];

this.posMatPlaneCount = 3;
this.posMatType = "float32";
this.posMatDim = meshdim;

this.enableMesh = gShowMeshes;

this.useNurbs = 0;
Expand All @@ -32,37 +36,90 @@ function Mesh() {
this.hasNurbsMat = 0;
this.textureName = "";

this.textureCoordMat = new JitterMatrix(2, "float32", [10, 10]);;
this.nurbsMat = new JitterMatrix(3, "float32", [4, 4]);
this.positionMat = new JitterMatrix(3, "float32", [10, 10]);
this.boundingMat = new JitterMatrix(3, "float32", 10);
this.adjacentCellsMat = new JitterMatrix(3, "float32", 8);
this.nurbsMat = new JitterMatrix(this.posMatPlaneCount, this.posMatType, this.nurbsDim);
this.textureCoordMat = new JitterMatrix(2, this.posMatType, this.posMatDim);
this.positionMat = new JitterMatrix(this.posMatPlaneCount, this.posMatType, this.posMatDim);
this.boundingMat = new JitterMatrix(this.posMatPlaneCount, this.posMatType, 10);
this.adjacentCellsMat = new JitterMatrix(this.posMatPlaneCount, this.posMatType, 8);

// unused for now
this.setenable = function(val) {
if(this.meshFull) {
this.meshPoints.enable = this.enableMesh && val;
this.meshGrid.enable = this.enableMesh && val;
this.meshFull.enable = val;
this.nurbs.enable = this.enableMesh && this.useNurbs && val;
}
}

this.initMesh = function(dimensions, drawto, ID, useNurbs) {
this.initMesh = function(drawto, ID, useNurbs, saveDict_) {
this.ID = ID;

this.useNurbs = useNurbs;
this.currentWindowRatio = gWindowRatio;

var newDim = this.checkDimForNurbs(dimensions);
if (saveDict_) {
this.loadDataFromDict(saveDict_);
this.setMeshDim(this.posMatDim); // calculate and set matrices dimensions
this.loadMatrixFromDict(saveDict_);
} else {
this.setMeshDim(this.posMatDim); // calculate and set matrices dimensions
this.initPositionMat(); // fill vertex mat from scratch
}

this.initMeshPoints(drawto);
this.initMeshGrid(drawto);
this.initMeshFull(drawto);
this.initNurbs(drawto);

this.setMeshDim(newDim); // calculate and set matrices dimensions
this.initPositionMat(); // init vertex mat
if (this.useNurbs) {
this.assignControlMatToNurbs(); // assign vertex mat to nurbs
this.hasNurbsMat = 0;
} else {
this.assignPositionMatToMesh(); // assign vertex mat to mesh
}
this.assignPositionMatToMesh(); // assign vertex mat to mesh

this.calcMeshBoundsMat(); // calculate mesh boundaries
this.initTextureCoordMat(); // init texture coord mat
}

this.loadDataFromDict = function(dict) {
this.posMatPlaneCount = dict.get("positionMat"+this.ID+"::planecount");
this.posMatType = dict.get("positionMat"+this.ID+"::type");
this.posMatDim = dict.get("positionMat"+this.ID+"::dimensions");
}

this.loadMatrixFromDict = function(dict) {
var posMatDataOb = JSON.parse(dict.get("positionMatData").stringify());
//postln(JSON.stringify(posMatDataOb));

var posMatDataArray = posMatDataOb["positionMat"+this.ID];
//postln(JSON.stringify(posMatDataArray));

//var thisPositionsArray = dict.get("positionMat"+this.ID+"[1]");
var idx = 0;
for (var i=0; i<this.positionMat.dim[0]; i++) {
for (var j=0; j<this.positionMat.dim[1]; j++) {
//var thisCell = thisPositionsArray.get("positions")[j+i*this.positionMat.dim[0]].get(i+"_"+j);
var thisCell = posMatDataArray[idx++];
this.positionMat.setcell2d(i,j, thisCell);
}
}
// this.scaleMesh();
}

this.setMeshDim = function(newDim) {
if (newDim[0] > 0 && newDim[1] > 0) {
this.positionMat.dim = newDim.slice();
this.boundingMat.dim = newDim[0]*2 + (newDim[1] * 2) - 4;
if (this.useNurbs) {
this.textureCoordMat.dim = this.nurbsDim.slice();
} else {
this.textureCoordMat.dim = newDim.slice();
}
}
else {
this.positionMat.dim = [4,4];
this.boundingMat.dim = 4*2 + (4 * 2) - 4;
this.textureCoordMat.dim = [4, 4];
}
}

this.changeMode = function(mode) {
if (mode != this.useNurbs) {
this.useNurbs = mode;
Expand Down Expand Up @@ -119,9 +176,9 @@ function Mesh() {
this.nurbs.matrixoutput = 1;
this.nurbs.drawto = drawto_;
this.nurbs.enable = this.useNurbs * this.enableMesh;
this.nurbs.name = "nurbs_"+this.ID;
//this.nurbs.name = "nurbs_"+this.ID;

this.nurbsMat = new JitterMatrix(3, "float32", this.nurbs.dim.slice());
this.nurbsMat = new JitterMatrix(this.posMatPlaneCount, this.posMatType, this.nurbs.dim.slice());

this.nurbsLstnr = new JitterListener(this.nurbs.name, nurbscallback);
nurbsmap[this.nurbs.name] = this;
Expand All @@ -148,28 +205,16 @@ function Mesh() {
this.meshPoints.enable = show;
}

this.setMeshDim = function(dimensions)
{
if (dimensions[0] > 0 && dimensions[1] > 0) {
this.positionMat.dim = dimensions.slice();
this.boundingMat.dim = dimensions[0]*2 + (dimensions[1] * 2) - 4;
if (this.useNurbs) {
this.textureCoordMat.dim = this.nurbsDim.slice();
} else {
this.textureCoordMat.dim = dimensions.slice();
}
}
else {
this.positionMat.dim = [4,4];
this.boundingMat.dim = 4*2 + (4 * 2) - 4;
this.textureCoordMat.dim = [4, 4];
}
this.getPositionMatProperties = function(prop) {
prop.type = this.positionMat.type;
prop.dim = this.positionMat.dim;
prop.planes = this.positionMat.planecount;
}

this.resizeMesh = function(dimensions) {
var newDim = this.checkDimForNurbs(dimensions);

var tempMat = new JitterMatrix(3, "float32", newDim);
var tempMat = new JitterMatrix(this.positionMat.planecount, this.positionMat.type, newDim);
tempMat.interp = 1;

tempMat.frommatrix(this.positionMat);
Expand All @@ -179,22 +224,21 @@ function Mesh() {
this.positionMat.frommatrix(tempMat);
tempMat.freepeer();

if (this.useNurbs) {
this.assignControlMatToNurbs(); // set the new position as the control matrix for nurbs
this.hasNurbsMat = 0;
} else {
this.assignPositionMatToMesh();
}
this.assignPositionMatToMesh();

this.initTextureCoordMat();
this.calcMeshBoundsMat();
}


this.assignPositionMatToMesh = function() {
this.meshPoints.vertex_matrix(this.positionMat.name);
this.meshGrid.vertex_matrix(this.positionMat.name);
this.meshFull.vertex_matrix(this.positionMat.name);
if (this.useNurbs) {
this.assignControlMatToNurbs(); // set the new position as the control matrix for nurbs
this.hasNurbsMat = 0;
} else {
this.meshPoints.vertex_matrix(this.positionMat.name);
this.meshGrid.vertex_matrix(this.positionMat.name);
this.meshFull.vertex_matrix(this.positionMat.name);
}
}

this.assignNurbsMatToMesh = function() {
Expand All @@ -216,11 +260,9 @@ function Mesh() {
this.meshFull.jit_gl_texture(this.textureName);
}

//-------------------------------------------

this.initTextureCoordMat = function() {
var xStartingPoint = (1.0/meshes) * this.ID;
var xCoordTarget = xStartingPoint + (1.0/meshes); // 0 a 1. +0.25
var xStartingPoint = (1.0/meshcount) * this.ID;
var xCoordTarget = xStartingPoint + (1.0/meshcount); // 0 a 1. +0.25
for (var i=0; i<this.textureCoordMat.dim[0]; i++)
{
for (var j=0; j<this.textureCoordMat.dim[1]; j++)
Expand All @@ -237,8 +279,8 @@ function Mesh() {
for(var i=0; i<this.positionMat.dim[0]; i++) {
for(var j=0; j<this.positionMat.dim[1]; j++) {
var xVal = (i / (this.positionMat.dim[0]-1));
xVal = map(xVal, 0., 1., -this.currentWindowRatio + ((this.currentWindowRatio / (meshes/2)) * this.ID),
-this.currentWindowRatio+(this.currentWindowRatio/(meshes/2)) + (this.currentWindowRatio / (meshes/2)) * this.ID);
xVal = map(xVal, 0., 1., -this.currentWindowRatio + ((this.currentWindowRatio / (meshcount/2)) * this.ID),
-this.currentWindowRatio+(this.currentWindowRatio/(meshcount/2)) + (this.currentWindowRatio / (meshcount/2)) * this.ID);
var yVal = ((j / (this.positionMat.dim[1]-1)) * 2.0) - 1.0;

this.positionMat.setcell2d(i, j, xVal, yVal, 0.0);
Expand All @@ -258,12 +300,9 @@ function Mesh() {
}
}
this.currentWindowRatio = gWindowRatio;
if (this.useNurbs) {
this.assignControlMatToNurbs();
this.hasNurbsMat = 0;
} else {
this.assignPositionMatToMesh();
}

this.assignPositionMatToMesh();

this.calcMeshBoundsMat();
}

Expand Down Expand Up @@ -295,13 +334,9 @@ function Mesh() {
this.moveVertex = function(coordsWorld, cellIndex) {
if (isPointInsidePolygon(coordsWorld, this.adjacentCellsMat)) {
this.setVertexPosInMat(coordsWorld, cellIndex);
if (this.useNurbs) {
// this.assignNurbsMatToMesh();
this.hasNurbsMat = 0;
} else {
this.assignPositionMatToMesh();
}
this.assignControlMatToNurbs();

this.assignPositionMatToMesh();

gGraphics.drawCircle(coordsWorld);
}
}
Expand Down Expand Up @@ -428,5 +463,17 @@ function Mesh() {
}
return [sizeX, sizeY];
}

this.positionMatToArray = function() {
var posArray = [];
for (var i=0; i<this.positionMat.dim[0]; i++) {
for (var j=0; j<this.positionMat.dim[1]; j++) {
//var thisPos = new Dict();
posArray.push(this.positionMat.getcell(i,j));
//posArrayDict.append("positions", thisPos);
}
}
return posArray;
}
}

Loading

0 comments on commit a2bc274

Please sign in to comment.