1 /**
  2  * potree.js 
  3  * http://potree.org
  4  *
  5  * Copyright 2012, Markus Sch�tz
  6  * Licensed under the GPL Version 2 or later.
  7  * - http://potree.org/wp/?page_id=7
  8  * - http://www.gnu.org/licenses/gpl-3.0.html
  9  *
 10  */
 11 
 12 /**
 13  * Some types of possible point attributes
 14  * 
 15  * @class
 16  */
 17 var PointAttributeNames = 
 18 {
 19 	POSITION_CARTESIAN 	: 0,	// float x, y, z;
 20 	COLOR_PACKED		: 1,	// byte r, g, b, a; 	I = [0,1]
 21 	COLOR_FLOATS_1		: 2,	// float r, g, b; 		I = [0,1]
 22 	COLOR_FLOATS_255	: 3,	// float r, g, b; 		I = [0,255]
 23 	NORMAL_FLOATS		: 4,  	// float x, y, z;
 24 	FILLER				: 5
 25 };
 26 
 27 var i = 0;
 28 for(obj in PointAttributeNames){
 29 	PointAttributeNames[i] = PointAttributeNames[obj];
 30 	i++;
 31 }
 32 
 33 /**
 34  * Some types of possible point attribute data formats
 35  * 
 36  * @class
 37  */
 38 var PointAttributeTypes =
 39 {
 40 	DATA_TYPE_DOUBLE	: {ordinal : 0, size: 8},
 41 	DATA_TYPE_FLOAT		: {ordinal : 1, size: 4},
 42 	DATA_TYPE_INT8		: {ordinal : 2, size: 1},
 43 	DATA_TYPE_UINT8		: {ordinal : 3, size: 1},
 44 	DATA_TYPE_INT16		: {ordinal : 4, size: 2},
 45 	DATA_TYPE_UINT16	: {ordinal : 5, size: 2},
 46 	DATA_TYPE_INT32		: {ordinal : 6, size: 4},
 47 	DATA_TYPE_UINT32	: {ordinal : 7, size: 4},
 48 	DATA_TYPE_INT64		: {ordinal : 8, size: 8},
 49 	DATA_TYPE_UINT64	: {ordinal : 9, size: 8}
 50 };
 51 
 52 var i = 0;
 53 for(obj in PointAttributeTypes){
 54 	PointAttributeTypes[i] = PointAttributeTypes[obj];
 55 	i++;
 56 }
 57 
 58 /**
 59  * A single point attribute such as color/normal/.. and its data format/number of elements/... 
 60  * 
 61  * @class
 62  * @param name 
 63  * @param type
 64  * @param size
 65  * @returns
 66  */
 67 function PointAttribute(name, type, numElements){
 68 	this.name = name;
 69 	this.type = type; 
 70 	this.numElements = numElements;
 71 	this.byteSize = this.numElements * this.type.size;
 72 }
 73 
 74 PointAttribute.POSITION_CARTESIAN = new PointAttribute(
 75 		PointAttributeNames.POSITION_CARTESIAN,
 76 		PointAttributeTypes.DATA_TYPE_FLOAT, 3);
 77 
 78 PointAttribute.RGBA_PACKED = new PointAttribute(
 79 		PointAttributeNames.COLOR_PACKED,
 80 		PointAttributeTypes.DATA_TYPE_INT8, 4);
 81 
 82 PointAttribute.COLOR_PACKED = PointAttribute.RGBA_PACKED;
 83 
 84 PointAttribute.RGB_PACKED = new PointAttribute(
 85 		PointAttributeNames.COLOR_PACKED,
 86 		PointAttributeTypes.DATA_TYPE_INT8, 3);
 87 
 88 PointAttribute.NORMAL_FLOATS = new PointAttribute(
 89 		PointAttributeNames.NORMAL_FLOATS,
 90 		PointAttributeTypes.DATA_TYPE_FLOAT, 3);
 91 
 92 PointAttribute.FILLER_1B = new PointAttribute(
 93 		PointAttributeNames.FILLER,
 94 		PointAttributeTypes.DATA_TYPE_UINT8, 1);
 95 
 96 /**
 97  * Ordered list of PointAttributes used to identify how points are aligned in a buffer.
 98  * 
 99  * @class
100  * 
101  */
102 function PointAttributes(pointAttributes){
103 	this.attributes = new Array();
104 	this.byteSize = 0;
105 	this.size = 0;
106 	
107 	if(pointAttributes != null){
108 		// does not work in chrome v24
109 //		for(var pointAttribute of pointAttributes){
110 //			this.attributes.push(pointAttribute);
111 //			this.byteSize += pointAttribute.byteSize;
112 //			this.size++;
113 //		}
114 		
115 		for(var name in pointAttributes){
116 			var pointAttribute = pointAttributes[name];
117 			this.attributes.push(pointAttribute);
118 			this.byteSize += pointAttribute.byteSize;
119 			this.size++;
120 		}
121 	}
122 };
123 
124 PointAttributes.prototype.add = function(pointAttribute){
125 	this.attributes.push(pointAttribute);
126 	this.byteSize += pointAttribute.byteSize;
127 	this.size++;
128 };
129 
130 PointAttributes.prototype.hasColors = function(){
131 	for(var name in this.attributes){
132 		var pointAttribute = this.attributes[name];
133 		if(pointAttribute.name == PointAttributeNames.COLOR_PACKED){
134 			return true;
135 		}
136 	}
137 	
138 	return false;
139 };
140 
141 PointAttributes.prototype.hasNormals = function(){
142 	for(var name in this.attributes){
143 		var pointAttribute = this.attributes[name];
144 		if(pointAttribute == PointAttribute.NORMAL_FLOATS){
145 			return true;
146 		}
147 	}
148 	
149 	return false;
150 };
151 
152 
153