view src/samer/j3d/Patches.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
package	samer.j3d;

import samer.core.types.*;
import samer.maths.*;
import java.util.*;
import javax.media.j3d.*;
import javax.vecmath.*;


public class Patches extends QuadArray implements Observer
{
	int			N;	// number of patches
	Matrix		P;	// positions and sizes of patches
	VVector		A;	// scalar activations
	VDouble		K;  // scaling factor for activations
	VDouble		L;  // scaling factor for sizes

	float		carray[];	// array for holding colours

	public Patches(Matrix points, VVector activities)
	{
		super(4*points.getRowDimension(), COORDINATES | COLOR_3);

		N=points.getRowDimension();
		P=points;
		A=activities;
		K=new VDouble("scale",1);
		L=new VDouble("patch.size",1);

		carray=new float[4*3*N];

		// set capabilities for subsequent updates
		setCapability(ALLOW_COORDINATE_WRITE);
		setCapability(ALLOW_COLOR_WRITE);

		updatePoints();
		updateActivities();

		A.addObserver(this);
		L.addObserver(new Observer () {
			public void update(Observable o, Object a) {
				updatePoints();
			}
		});
	}


	public void update(Observable o, Object a)
	{
		updateActivities();
	}

	private double quad[] = new double[12];

	public void updatePoints()
	{
		// load points into point array
		double [][] PA=P.getArray();
		double []	row;
		double		l=L.value;
		int			j=0;

		for (int i=0; i<N; i++) {
			row=PA[i];
			double x1=row[0]-l*row[1];
			double x2=row[0]+l*row[1];
			double y1=row[3]-l*row[4];
			double y2=row[3]+l*row[4];

			quad[0] = x1;	quad[1] = y1;	quad[2] = 0;
			quad[3] = x1;	quad[4] = y2;	quad[5] = 0;
			quad[6] = x2;	quad[7] = y2;	quad[8] = 0;
			quad[9] = x2;	quad[10]= y1;	quad[11]= 0;

			// set 4 vertices of quad
			setCoordinates(j,quad,0,4); j+=4;
		}
	}

	public void updateActivities()
	{
		// load colours
		double []	a=A.array();
		double		k=K.value;
		int			j=0;

		for (int i=0; i<N; i++) {
			float ai=(float)(a[i]*k);

			carray[j]=ai; carray[j+1]=ai;	carray[j+2]=ai; j+=3;
			carray[j]=ai; carray[j+1]=ai;	carray[j+2]=ai; j+=3;
			carray[j]=ai; carray[j+1]=ai;	carray[j+2]=ai; j+=3;
			carray[j]=ai; carray[j+1]=ai;	carray[j+2]=ai; j+=3;
		}
		setColors(0,carray);
	}
}