Source

LuaTools / Tools / vmath.lua

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831

module(..., package.seeall);

local function ClassifyTable(theTable, theMembers, metaTable)
	for funcName, func in pairs(theMembers) do
		theTable[funcName] = func;
	end
	
	if(metaTable) then setmetatable(theTable, metaTable) end
	
	return theTable;
end

------------------------------
-- vmath type system.
local function SetVmathType(theVal, theType)
	theVal._vtype = theType;
end

local function GetVmathType(theValue)
	if(type(theValue) ~= "table") then return type(theValue) end
	
	if(theValue._vtype) then return theValue._vtype end
	
	return "table";
end

local function IsVmathVector(theValue)
	if(string.match(GetVmathType(theValue), "vec.")) then
		return GetVmathType(theValue);
	else
		return false
	end
end

local function GetVmathTypeLength(theValue)
	local theType = GetVmathType(theValue);
	
	local match = string.match(theType, "vec(.)")
	if(match) then return tonumber(match), "vec" end
	
	match = string.match(theType, "mat(.)")
	if(match) then return tonumber(match), "mat" end
	
	return nil, theType;
end

------------------------------
-- Metatable helpers.
local vectorIndexers = {"x", "y", "z", "w"}
local vectorRevIndexers = {x=1, y=2, z=3, w=4}

local function VectorIndex(theVec, key, vecSize)
	local revValue = vectorRevIndexers[key];
	if(revValue and revValue <= vecSize) then
		return rawget(theVec, revValue);
	end
	
	return rawget(theVec, key);
end

local function VectorNewIndex(theVec, key, value, vecSize)
	if(vectorIndexers[key] and key <= vecSize) then
		return rawset(theVec, key, value);
	end

	local revValue = vectorRevIndexers[key];
	if(revValue and revValue <= vecSize) then
		return rawset(theVec, revValue, value);
	end
end

local function VectorOperatorCall(lhs, rhs, same, leftIsVec, rightIsVec)
	local lhsType = GetVmathType(lhs);
	local rhsType = GetVmathType(rhs);
	
	if(lhsType == rhsType) then
		--Both must be vectors.
		return same(lhs, rhs);
	else
		--Find the vector.
		if(lhsType == "number") then
			if(not rightIsVec) then
				return leftIsVec(rhs, lhs);
			else
				return rightIsVec(rhs, lhs);
			end
		else
			assert(rhsType == "number", "You cannot operate a " .. lhsType ..
				" to a " .. rhsType .. ".");
			return leftIsVec(lhs, rhs);
		end
	end
end

------------------------------
-- Matrix helpers
local matrixRowIndexers = {"x", "y", "z", "w"}
local matrixColIndexers = {"cx", "cy", "cz", "cw"}
local matrixRowRevIndexers = {x=1, y=2, z=3, w=4}
local matrixColRevIndexers = {cx=1, cy=2, cz=3, cw=4}

local function MatrixIndex(theMat, key, matSize)
	local revValue = matrixRowRevIndexers[key];
	if(revValue and revValue <= matSize) then
		return rawget(theMat, revValue);
	end
	
	revValue = matrixColRevIndexers[key];
	if(revValue and revVale <= matSize) then
		return theMat:col(revValue);
	end
	
	return rawget(theMat, key);
end

local function MatrixNewIndex(theMat, key, value, matSize)
	assert(GetVmathTypeLength(value) == matSize,
		"The vector type " .. GetVmathType(theMat) ..
		" cannot be set on a mat" ..	matSize .. ".")

	if(matrixRowIndexers[key] and key <= vecSize) then
		return rawset(theVec, key, value);
	end

	local revValue = matrixRowRevIndexers[key];
	if(revValue and revValue <= matSize) then
		return rawset(theMat, revValue, value);
	end
	
	revValue = matrixColRevIndexers[key];
	if(revValue and revVale <= matSize) then
		return theMat:SetCol(revValue, value);
	end
end

local function MatrixMultiply(lhs, rhs, matSize)
	local rhsLen, rhsType = GetVmathTypeLength(rhs);
	assert(rhsType == "mat", "Attempted to multiply a matrix by a " .. rhsType);
	assert(rhsLen == matSize, "Trying to multiply matrix of size " .. matSize ..
		" by a matrix of size " .. rhsLen);
	
	local cols = rhs:cols();
	
	local out = {}
	for i = 1, matSize do
		local row = {};
		for j = 1, matSize do
			row[j] = lhs[i]:dot(cols[j])
		end
		
		out[#out + 1] = vector(row);
	end
	
	return matrix(unpack(out));
end




------------------------------
-- Vectoral helpers.
local function AddVecScalar(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector, i) + theNumber;
	end

	return unpack(outVec)
end

local function MulVecScalar(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector, i) * theNumber;
	end

	return unpack(outVec)
end

local function SubVecScalar(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector, i) - theNumber;
	end

	return unpack(outVec)
end

local function SubScalarVec(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = theNumber - rawget(vector, i);
	end

	return unpack(outVec)
end

local function DivVecScalar(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector, i) / theNumber;
	end

	return unpack(outVec)
end

local function DivScalarVec(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = theNumber / rawget(vector, i);
	end

	return unpack(outVec)
end

local function ModVecScalar(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector, i) % theNumber;
	end

	return unpack(outVec)
end

local function ModVecScalar(vector, theNumber, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = theNumber % rawget(vector, i);
	end

	return unpack(outVec)
end


local function AddVector(vector1, vector2, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector1, i) + rawget(vector2, i);
	end

	return unpack(outVec)
end

local function MulVector(vector1, vector2, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector1, i) * rawget(vector2, i);
	end

	return unpack(outVec)
end

local function SubVector(vector1, vector2, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector1, i) - rawget(vector2, i);
	end

	return unpack(outVec)
end

local function DivVector(vector1, vector2, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector1, i) / rawget(vector2, i);
	end

	return unpack(outVec)
end

local function ModVector(vector1, vector2, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = rawget(vector1, i) % rawget(vector2, i);
	end

	return unpack(outVec)
end

local function NegVector(vector, size)
	local outVec = {}
	for i = 1, size, 1 do
		outVec[i] = -rawget(vector, i);
	end

	return unpack(outVec)
end

local function DotDirect(lhs, rhs, size)
	local ret = 0;
	for i=1, size do
		ret = ret + (rawget(lhs, i) * rawget(rhs, i));
	end
	
	return ret;
end

local function DotProduct(lhs, rhs)
	local lhsLen, lhsType = GetVmathTypeLength(lhs);
	assert(lhsType and lhsType == "vec", "LHS of 'dot' is not a vector.");
	
	local rhsLen, rhsType = GetVmathTypeLength(rhs);
	assert(rhsLen and rhsType == "vec", "RHS of 'dot' should be a vector. It is a " .. rhsType);
	assert(rhsLen == lhsLen, "LHS and RHS of 'dot' must be the same length. " ..
		"LHS = " .. lhsLen .. " RHS = " .. rhsLen);
	
	return DotDirect(lhs, rhs, rhsLen);
end





------------------------------
-- Vec2 Member Functions
local vec2Members = {};

vec2Members.dot = DotProduct;

function vec2Members.cross(this)
	return vec2(rawget(this, 2), -rawget(this, 1));
end

------------------------------
-- Vec3 Member Functions
local vec3Members = {};

vec3Members.dot = DotProduct;

function vec3Members.cross(lhs, rhs)
	return vec3(
		(lhs.y * rhs.z) - (lhs.z * rhs.y),
		(lhs.z * rhs.x) - (lhs.x * rhs.z),
		(lhs.x * rhs.y) - (lhs.y * rhs.x));
end

------------------------------
-- Vec4 Member Functions
local vec4Members = {};

vec4Members.dot = DotProduct;

function vec4Members.cross(lhs, mhs, rhs)
	function row(i) return vec3(lhs[i], mhs[i], rhs[i]) end
	function detcross(i, j, k)
		return dot(row(i), row(j):cross(row(k)))
	end
	
	return vec4(
		detcross(2, 3, 4),
		-detcross(1, 3, 4),
		detcross(1, 2, 4),
		-detcross(1, 2, 3));
end

------------------------------
-- Mat3 Member Functions
local mat3Members = {};

function mat3Members.col(this, colIndex)
	assert(0 < colIndex and colIndex <= 3, "3x3 Matrices only have 3 rows.");
	return vec3(rawget(this, 1)[colIndex],
		rawget(this, 2)[colIndex],
		rawget(this, 3)[colIndex]);
end

function mat3Members.cols(this)
	return {this:col(1), this:col(2), this:col(3)}
end

function mat3Members.SetCol(this, colIndex, theCol)
	assert(0 < colIndex and colIndex <= 3, "3x3 Matrices only have 3 columns.");
	assert(GetVmathType(theCol) == "vec3", "You can only set vec3s into mat3s.");
	
	for i = 1, 3, 1 do
		local temp = rawget(this, i);
		temp[colIndex] = theCol[i]
		rawset(this, i, temp);
	end
end

function mat3Members.Transform(theMatrix, theVector)
	local matLen, matType = GetVmathTypeLength(theMatrix);
	assert(matLen and matType == "mat", "Transform is a method; use ':' instead of '.'.");
	
	local vecLen, vecType = GetVmathTypeLength(theVector);
	assert(vecLen and vecType == "vec", "The second parameter of Transform must be a vector. It is a " .. vecType .. ".");
	
	--Affine transform.
	local bIsAffine = false;
	if(matLen == vecLen + 1) then
		vecLen = vecLen + 1;
		theVector = vector(theVector, 1.0)
		bIsAffine = true;
	end
	
	assert(vecLen == matLen, "The vector and matrix of 'Transform' are not the same.");
	
	local outVec = {}
	for i=1, matLen do
		outVec[i] = DotDirect(theVector, theMatrix[i], vecLen)
	end
	
	if(bIsAffine) then outVec[matLen] = nil end;

	return vector(outVec);
end

------------------------------
-- Mat4 Member Functions
local mat4Members = {};

function mat4Members.col(this, colIndex)
	assert(0 < colIndex and colIndex <= 4, "4x4 Matrices only have 4 rows.");
	return vec4(rawget(this, 1)[colIndex],
		rawget(this, 2)[colIndex],
		rawget(this, 3)[colIndex],
		rawget(this, 4)[colIndex]);
end

function mat4Members.cols(this)
	return {this:col(1), this:col(2), this:col(3), this:col(4)}
end

mat4Members.Transform = mat3Members.Transform;




------------------------------
-- Vec2 MetaTable
local vec2MetaTable = {};

function vec2MetaTable.__add(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec2(AddVector(a, b, 2)) end,
		function(a, b) return vec2(AddVecScalar(a, b, 2)) end,
		nil)
end

function vec2MetaTable.__sub(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec2(SubVector(a, b, 2)) end,
		function(a, b) return vec2(SubVecScalar(a, b, 2)) end,
		function(a, b) return vec2(SubScalarVec(a, b, 2)) end)
end

function vec2MetaTable.__mul(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec2(MulVector(a, b, 2)) end,
		function(a, b) return vec2(MulVecScalar(a, b, 2)) end,
		nil)
end

function vec2MetaTable.__div(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec2(DivVector(a, b, 2)) end,
		function(a, b) return vec2(DivVecScalar(a, b, 2)) end,
		function(a, b) return vec2(DivScalarVec(a, b, 2)) end)
end

function vec2MetaTable.__mod(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec2(ModVector(a, b, 2)) end,
		function(a, b) return vec2(ModVecScalar(a, b, 2)) end,
		function(a, b) return vec2(ModScalarVec(a, b, 2)) end)
end

function vec2MetaTable.__unm(vector)
	return vec2(NegVector(vector, 2))
end

function vec2MetaTable.__index(theVec, key)
	return VectorIndex(theVec, key, 2);
end

function vec2MetaTable.__newindex(theVec, key, value)
	return VectorNewIndex(theVec, key, value, 2);
end

function vec2MetaTable.__tostring(theVec)
	return "(" .. table.concat(theVec, ", ") .. ")";
end


------------------------------
-- Vec3 MetaTable
local vec3MetaTable = {};

function vec3MetaTable.__add(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec3(AddVector(a, b, 3)) end,
		function(a, b) return vec3(AddVecScalar(a, b, 3)) end,
		nil)
end

function vec3MetaTable.__sub(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec3(SubVector(a, b, 3)) end,
		function(a, b) return vec3(SubVecScalar(a, b, 3)) end,
		function(a, b) return vec3(SubScalarVec(a, b, 3)) end)
end

function vec3MetaTable.__mul(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec3(MulVector(a, b, 3)) end,
		function(a, b) return vec3(MulVecScalar(a, b, 3)) end,
		nil)
end

function vec3MetaTable.__div(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec3(DivVector(a, b, 3)) end,
		function(a, b) return vec3(DivVecScalar(a, b, 3)) end,
		function(a, b) return vec3(DivScalarVec(a, b, 3)) end)
end

function vec3MetaTable.__mod(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec3(ModVector(a, b, 3)) end,
		function(a, b) return vec3(ModVecScalar(a, b, 3)) end,
		function(a, b) return vec3(ModScalarVec(a, b, 3)) end)
end

function vec3MetaTable.__unm(vector)
	return vec3(NegVector(vector, 3))
end

function vec3MetaTable.__index(theVec, key)
	return VectorIndex(theVec, key, 3);
end

function vec3MetaTable.__newindex(theVec, key, value)
	return VectorNewIndex(theVec, key, value, 3);
end

function vec3MetaTable.__tostring(theVec)
	return "(" .. table.concat(theVec, ", ") .. ")";
end


------------------------------
-- Vec4 MetaTable
local vec4MetaTable = {};

function vec4MetaTable.__add(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec4(AddVector(a, b, 4)) end,
		function(a, b) return vec4(AddVecScalar(a, b, 4)) end,
		nil)
end

function vec4MetaTable.__sub(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec4(SubVector(a, b, 4)) end,
		function(a, b) return vec4(SubVecScalar(a, b, 4)) end,
		function(a, b) return vec4(SubScalarVec(a, b, 4)) end)
end

function vec4MetaTable.__mul(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec4(MulVector(a, b, 4)) end,
		function(a, b) return vec4(MulVecScalar(a, b, 4)) end,
		nil)
end

function vec4MetaTable.__div(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec4(DivVector(a, b, 4)) end,
		function(a, b) return vec4(DivVecScalar(a, b, 4)) end,
		function(a, b) return vec4(DivScalarVec(a, b, 4)) end)
end

function vec4MetaTable.__mod(lhs, rhs)
	return VectorOperatorCall(lhs, rhs,
		function(a, b) return vec4(ModVector(a, b, 4)) end,
		function(a, b) return vec4(ModVecScalar(a, b, 4)) end,
		function(a, b) return vec4(ModScalarVec(a, b, 4)) end)
end

function vec4MetaTable.__unm(vector)
	return vec4(NegVector(vector, 4))
end

function vec4MetaTable.__index(theVec, key)
	return VectorIndex(theVec, key, 4);
end

function vec4MetaTable.__newindex(theVec, key, value)
	return VectorNewIndex(theVec, key, value, 4);
end

function vec4MetaTable.__tostring(theVec)
	return "(" .. table.concat(theVec, ", ") .. ")";
end


-----------------------------------
-- Mat3 Meta Table 
local mat3MetaTable = {};

function mat3MetaTable.__mul(lhs, rhs)
	return MatrixMultiply(lhs, rhs, 3);
end

function mat3MetaTable.__index(theMat, key)
	return MatrixIndex(theMat, key, 3);
end

function mat3MetaTable.__newindex(theMat, key, value)
	return MatrixNewIndex(theMat, key, value, 3);
end

function mat3MetaTable.__tostring(theMat)
	local strRows = {}
	for i, row in ipairs(theMat) do
		strRows[#strRows + 1] = tostring(row);
	end
	return "[\t" .. table.concat(strRows, ",\n\t") .. "]";
end

-----------------------------------
-- Mat4 Meta Table 
local mat4MetaTable = {};

function mat4MetaTable.__mul(lhs, rhs)
	return MatrixMultiply(lhs, rhs, 4);
end

function mat4MetaTable.__index(theMat, key)
	return MatrixIndex(theMat, key, 4);
end

function mat4MetaTable.__newindex(theMat, key, value)
	return MatrixNewIndex(theMat, key, value, 4);
end

mat4MetaTable.__tostring = mat3MetaTable.__tostring;





-----------------------------------
-- Constructors

local function ParseVectorInputs(remainingValues, first, ...)
	if(remainingValues == 0) then return nil end;
	if(not first) then return nil end;
	
	local vecLen, theType = GetVmathTypeLength(first);

	if(theType ~= "vec") then
		if(theType == "table" and #first ~= 0) then
			local ret = {}
			local others = { ParseVectorInputs(remainingValues - #first, ...) }
			for i, value in ipairs(first) do ret[#ret + 1] = value end
			for i, value in ipairs(others) do ret[#ret + 1] = value end
			return unpack(ret);
		else
			return first, ParseVectorInputs(remainingValues - 1, ...)
		end
	end
	
	local ret = { unpack(first) }
	local others = { ParseVectorInputs(remainingValues - #first, ...) }
	for i, value in ipairs(others) do ret[#ret + 1] = value end
	return unpack(ret);
end

function vec2(...)
	local vec = {ParseVectorInputs(2, ...)}
	
	if(not vec[2]) then
		if(not vec[1]) then
			vec[1] = 0.0; vec[2] = 0.0;
		else
			if(GetVmathType(vec[1]) == "number") then
				vec[2] = vec[1];
			else
				
			end
		end
	end
	
	SetVmathType(vec, "vec2");
	
	return ClassifyTable(vec, vec2Members, vec2MetaTable);
end

function vec3(...)
	local vec = {ParseVectorInputs(3, ...)}
	
	if(not vec[2] and not vec[3]) then
		if(not vec[1]) then
			vec[1] = 0.0; vec[2] = 0.0; vec[3] = 0.0;
		else
			vec[2] = vec[1]; vec[3] = vec[1];
		end
	end
	
	assert(vec[3], "You must pass either 0, 1, or 3 values to vec3's constructor");
	
	SetVmathType(vec, "vec3");
	
	return ClassifyTable(vec, vec3Members, vec3MetaTable);
end

function vec4(...)
	local vec = {ParseVectorInputs(4, ...)}
	
	if(not vec[2] and not vec[3] and not vec[4]) then
		if(not vec[1]) then
			vec[1] = 0.0; vec[2] = 0.0; vec[3] = 0.0; vec[4] = 0.0;
		else
			vec[2] = vec[1]; vec[3] = vec[1]; vec[4] = vec[1];
		end
	end
	
	assert(vec[3] and vec[4], "You must pass either 0, 1, or 4 values to vec4's constructor. You passed " .. #vec .. ".");
	
	SetVmathType(vec, "vec4");
	
	return ClassifyTable(vec, vec4Members, vec4MetaTable);
end

local vectorTable = {
	[2] = {type="vec2", members=vec2Members, metatable=vec2MetaTable},
	[3] = {type="vec3", members=vec3Members, metatable=vec3MetaTable},
	[4] = {type="vec4", members=vec4Members, metatable=vec4MetaTable},
}

function vector(...)
	local vec = {ParseVectorInputs(4, ...)}
	
	assert(vectorTable[#vec], "You must pass vector parameters of length (1, 4]. You passed " .. #vec);
	
	SetVmathType(vec, vectorTable[#vec].type);
	return ClassifyTable(vec, vectorTable[#vec].members, vectorTable[#vec].metatable);
end

function mat3(row1, row2, row3)
	local rows = {row1, row2, row3}
	
	if(not rows[2] and not rows[3]) then
		if(not rows[1]) then
			rows = {vec3(), vec3(), vec3()}
		else
			rows = {rows[1], rows[1], rows[1]}
		end
	end

	SetVmathType(rows, "mat3");
	
	return ClassifyTable(rows, mat3Members, mat3MetaTable);
end

function mat4(row1, row2, row3, row4)
	local rows = {row1, row2, row3, row4}
	
	if(not rows[2] and not rows[3] and not rows[4]) then
		if(not rows[1]) then
			rows = {vec4(), vec4(), vec4(), vec4()}
		else
			rows = {rows[1], rows[1], rows[1], rows[1]}
		end
	end

	SetVmathType(rows, "mat4");
	
	return ClassifyTable(rows, mat4Members, mat4MetaTable);
end

function matrix(...)
	local row1 = ...;
	local vecLen = GetVmathTypeLength(row1);
	
	assert(vecLen == 3 or vecLen == 4, "You can only construct a matrix from 3d or 4d vectors.");
	
	if(vecLen == 3) then return mat3(...) else return mat4(...) end
end

------------------------------
-- Typiung
function vtype(theValue)
	return GetVmathType(theValue)
end

------------------------------
-- Other global functions
dot = DotProduct;
function dot(lhs, rhs)
	local lhsLen, lhsType = GetVmathTypeLength(lhs);
	assert(lhsType and lhsType == "vec", "LHS of 'dot' is not a vector.");
	
	local rhsLen, rhsType = GetVmathTypeLength(rhs);
	assert(rhsLen and rhsType == "vec", "RHS of 'dot' is not a vector.");
	assert(rhsLen == lhsLen, "LHS and RHS of 'dot' must be the same length.");
	
	return DotDirect(lhs, rhs, rhsLen);
end

function lenSqr(theVec)
	local vecLen, theType = GetVmathTypeLength(theVec)
	
	assert(theType == "vec", "You can only get the length of vectors.");

	local accum = 0;
	for i, value in ipairs(theVec) do
		accum = accum + (value * value);
	end
	
	return accum;
end

function length(theVec)
	return math.sqrt(lenSqr(theVec));
end

function norm(theVec)
	local theLen = length(theVec);
	if(theLen == 0) then return theVec end
	
	return theVec / theLen;
end

normalize = norm;