/*
 * $Id: Intensity.java,v 1.6 1999/09/28 13:48:07 yamauchi Exp $
 *     (M\pi)^2 : Intensity 
 *	Copyright 1999 By YAMAUCHI Hitoshi 山内 斉 
 *	@author YAMAUCHI Hitoshi 山内 斉
 *	
 * 輝度情報
 * original source Intensity.h
 * 
 * 1999年7月31日(土)
 */

package jp.ac.tohoku.is.archi.mpi2.obj;

import java.io.Serializable;
import jp.ac.tohoku.is.archi.mpi2.util.*;
import jp.ac.tohoku.is.archi.mpi2.sysparam.*;

public final class Intensity 
implements Mpi2Enum, Encodable
{
    private int 	mScreenX;
    private int 	mScreenY;
    private Vector3 	mIntensity;

    public static int getTAG() 
    { 
	return(TAG_INTENSITY);	
    }

    public Intensity()
    {
	this.mScreenX = -1;
	this.mScreenY = -1;
	this.mIntensity = null;
    }

    public Intensity(Intensity rhs)
    {
	this.mScreenX   = rhs.mScreenX;
	this.mScreenY   = rhs.mScreenY;
	this.mIntensity = new Vector3(rhs.mIntensity);
    }

    public Intensity(int x, int y,  Vector3 colorRGB)
    {
	this.mScreenX   = x;
	this.mScreenY   = y;
	this.mIntensity = colorRGB;
    }

    public void setScreenPos(int x, int y)
    {
	this.mScreenX = x;
	this.mScreenY = y;
    }

    public void setIntensityRGB(Vector3 color)
    {
	this.mIntensity = color;
    }

    public int getScreenX()  		
    {
	return(this.mScreenX);	
    }

    public int getScreenY()  		
    {
	return(this.mScreenY);	
    }

    public Vector3 peekIntensityRGB()  	
    {
	return(this.mIntensity); 	
    }
    
    /**
     * String representation of Intensity
     */
    public String toString()
    {
	String s = "Intensity (" + this.mScreenX + ", " + this.mScreenY +
	    ") = " + this.mIntensity;
	return(s);
    }


    /*
     * Implements Encodable
     */
    private static int mSizeInDouble = 
    	1 + 
    	1 + 
    	Vector3.ZERO.sizeInDouble(); // Vector3 mIntensity

    public int sizeInDouble() 
    {
	return(mSizeInDouble);
    }
    public void encodeInDouble(double[] array, int offset) 
    {
	array[offset] 		= (double)(this.mScreenX);
	array[offset + 1] 	= (double)(this.mScreenY);
	this.mIntensity.encodeInDouble(array, offset + 2);
    }

    /*
     * Implements ObjectDecoder
     */
    private static class IntensityObjectDecoder implements ObjectDecoder 
    {
    	public int sizeInDouble() 
	{
	    return(mSizeInDouble);
	}
    	public Object decodeFromDouble(double[] array, int offset)
	{
	    int scrX = (int)(array[offset]);
	    int scrY = (int)(array[offset + 1]);
	    ObjectDecoder v3od  = Vector3.getObjectDecoder();
	    Vector3 rgb = (Vector3)(v3od.decodeFromDouble(array, offset + 2));
	    
	    Intensity iy = new Intensity(scrX, scrY, rgb);
	    return(iy);
	}
    }
    private final static ObjectDecoder mODInstance 
    	= new IntensityObjectDecoder();
    public static ObjectDecoder getObjectDecoder() 
    {
	return(mODInstance);
    }


    public static void main(String [] args)
    {
	final int SENDNUM = 64;
	Intensity [] iarray = new Intensity[SENDNUM];
	for(int i = 0; i < SENDNUM; i++)
	{
	    double x = (double)i;
	    iarray[i] = 
		new Intensity(i, i, new Vector3(x + 0.1, x + 0.2, x + 0.3));
	}

	// send
	EncodedData sendbuf = Marshal.createEncodedData(iarray);
	double [] sendary = (double [])sendbuf.getArray();

	// recv
	EncodedData recvbuf = 
	    Marshal.createEncodedData(Intensity.getObjectDecoder(), 
				      SENDNUM);
	double [] recvary = (double [])recvbuf.getArray();
	for(int i = 0; i < sendary.length; i++)
	{
	    recvary[i] = sendary[i];
	}
	

	Decoder decoder = recvbuf.getDecoder();
	int recvnum = recvary.length / (new Intensity()).sizeInDouble();
	Intensity [] recInt = new Intensity[recvnum];
	for (int i = 0; i < recvnum; i++) 
	{
	    recInt[i] = 
		(Intensity)decoder.decode(Intensity.getObjectDecoder());
	}
	System.out.println("recvnum = " + recvnum);	

	for (int i = 0; i < recvnum; i++) 
	{
	    System.out.println("deserialized " + recInt[i]);
	}	
	// System.out.println("true " + true + ", false " + false);	
	// Class c = IntVec3.class;
    }
}
