/*
 * @(#)matrix3d.java        1.0 2002/08/01
 *
 * Copyright 2001-2006 by Danger Research, Inc.
 * 3101 Park Blvd, Palo Alto, CA  94301
 * All rights reserved.
 */
package com.danger.test3d;

import danger.util.*;


class matrix3d
{
	//~ Instance fields --------------------------------------------------------

	public int[] matrix; // single array

	//~ Constructors -----------------------------------------------------------

	/**
	 * Creates a new matrix3d object.
	 */
	public matrix3d()
	{
		matrix = new int[16];
	}

	//~ Methods ----------------------------------------------------------------

	/**
	 * DOCUMENT ME!
	 */
	public void Clear()
	{
		for (int i = 0; i < 16; i++)
		{
			matrix[i] = 0;
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param b DOCUMENT ME!
	 * @param result DOCUMENT ME!
	 */
	public void MultiplyBy(matrix3d b, matrix3d result)
	{
		for (int i = 0; i < 4; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				result.matrix[i + (j * 4)] = Fixed.multiply(matrix[0 + (j * 4)],
						b.matrix[i + 0]) +
					Fixed.multiply(matrix[1 + (j * 4)], b.matrix[i + (1 * 4)]) +
					Fixed.multiply(matrix[2 + (j * 4)], b.matrix[i + (2 * 4)]) +
					Fixed.multiply(matrix[3 + (j * 4)], b.matrix[i + (3 * 4)]);
			}
		}
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param p DOCUMENT ME!
	 * @param result DOCUMENT ME!
	 */
	public void MultiplyBy(point3d p, point3d result)
	{
		result.x     = Fixed.multiply(matrix[0 + (0 * 4)], p.x) +
			Fixed.multiply(matrix[1 + (0 * 4)], p.y) +
			Fixed.multiply(matrix[2 + (0 * 4)], p.z);
		result.y     = Fixed.multiply(matrix[0 + (1 * 4)], p.x) +
			Fixed.multiply(matrix[1 + (1 * 4)], p.y) +
			Fixed.multiply(matrix[2 + (1 * 4)], p.z);
		result.z     = Fixed.multiply(matrix[0 + (2 * 4)], p.x) +
			Fixed.multiply(matrix[1 + (2 * 4)], p.y) +
			Fixed.multiply(matrix[2 + (2 * 4)], p.z);
	}

	/**
	 * DOCUMENT ME!
	 */
	public void PrintFixed()
	{
		for (int j = 0; j < 4; j++)
		{
			DEBUG.p(Fixed.toInt(matrix[0 + (j * 4)]) + "." +
				Fixed.getFraction(matrix[0 + (j * 4)]) + " " +
				Fixed.toInt(matrix[1 + (j * 4)]) + "." +
				Fixed.getFraction(matrix[1 + (j * 4)]) + " " +
				Fixed.toInt(matrix[2 + (j * 4)]) + "." +
				Fixed.getFraction(matrix[2 + (j * 4)]) + " " +
				Fixed.toInt(matrix[3 + (j * 4)]) + "." +
				Fixed.getFraction(matrix[3 + (j * 4)]));
		}
	}
}
