/usr/src/castle-game-engine-4.1.1/castlescript/castlescriptvectors_implement_matrix.inc is in castle-game-engine-src 4.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | { Common implementation of matrix-based TCasScriptValue descendants.
Needs following macros defined before including:
MatrixGetCount
TCasScriptVecXx
TCasScriptMatrixXx
TMatrixXxx
RegisterMatrixXxFunctions
}
{ TCasScriptMatrixXx ---------------------------------------------------------- }
class procedure TCasScriptMatrixXx.HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
{ The function allows only >= 1 arguments, and this handler is
registered only for TCasScriptMatrixXx values, so we can safely take
the first arg as TCasScriptMatrixXx. }
TCasScriptMatrixXx(AResult).Value := TCasScriptMatrixXx(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptMatrixXx(AResult).Value := MatrixAdd(
TCasScriptMatrixXx(AResult).Value, TCasScriptMatrixXx(Arguments[I]).Value);
end;
class procedure TCasScriptMatrixXx.HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
TCasScriptMatrixXx(AResult).Value := TCasScriptMatrixXx(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptMatrixXx(AResult).Value := MatrixSubtract(
TCasScriptMatrixXx(AResult).Value, TCasScriptMatrixXx(Arguments[I]).Value);
end;
class procedure TCasScriptMatrixXx.HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
TCasScriptMatrixXx(AResult).Value := MatrixNegate(TCasScriptMatrixXx(Arguments[0]).Value);
end;
class procedure TCasScriptMatrixXx.HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
if (Length(Arguments) = 2) and
(Arguments[0] is TCasScriptFloat) and
(Arguments[1] is TCasScriptMatrixXx) then
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
TCasScriptMatrixXx(AResult).Value := MatrixMultScalar(
TCasScriptMatrixXx(Arguments[1]).Value,
TCasScriptFloat(Arguments[0]).Value);
end else
if (Length(Arguments) = 2) and
(Arguments[1] is TCasScriptFloat) and
(Arguments[0] is TCasScriptMatrixXx) then
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
TCasScriptMatrixXx(AResult).Value := MatrixMultScalar(
TCasScriptMatrixXx(Arguments[0]).Value,
TCasScriptFloat(Arguments[1]).Value);
end else
if (Length(Arguments) = 2) and
(Arguments[0] is TCasScriptMatrixXx) and
(Arguments[1] is TCasScriptVecXx) then
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVecXx);
TCasScriptVecXx(AResult).Value := MatrixMultVector(
TCasScriptMatrixXx(Arguments[0]).Value,
TCasScriptVecXx(Arguments[1]).Value);
end else
begin
{ So this is matrix * matrix... operation }
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
TCasScriptMatrixXx(AResult).Value := TCasScriptMatrixXx(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptMatrixXx(AResult).Value := MatrixMult(
TCasScriptMatrixXx(AResult).Value, TCasScriptMatrixXx(Arguments[I]).Value);
end;
end;
class procedure TCasScriptMatrixXx.HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
TCasScriptMatrixXx(AResult).Value := MatrixMultScalar(
TCasScriptMatrixXx(Arguments[0]).Value,
1/TCasScriptFloat(Arguments[1]).Value);
end;
class procedure TCasScriptMatrixXx.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
MatricesPerfectlyEqual(
TCasScriptMatrixXx(Arguments[0]).Value,
TCasScriptMatrixXx(Arguments[1]).Value);
end;
class procedure TCasScriptMatrixXx.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
not MatricesPerfectlyEqual(
TCasScriptMatrixXx(Arguments[0]).Value,
TCasScriptMatrixXx(Arguments[1]).Value);
end;
class procedure TCasScriptMatrixXx.HandleMatrix(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptMatrixXx);
for I := 0 to MatrixGetCount - 1 do
TCasScriptMatrixXx(AResult).FValue[I] := TCasScriptVecXx(Arguments[I]).Value;
TCasScriptMatrixXx(AResult).ValueAssigned := true;
end;
class procedure TCasScriptMatrixXx.HandleMatrixGet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
Index: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptVecXx);
Index := TCasScriptInteger(Arguments[1]).Value;
if not Between(Index, 0, MatrixGetCount - 1) then
raise ECasScriptError.CreateFmt('Invalid index %d for matrix_get on %d-column matrix',
[Index, MatrixGetCount]);
TCasScriptVecXx(AResult).Value := TCasScriptMatrixXx(Arguments[0]).Value[Index];
end;
class procedure TCasScriptMatrixXx.HandleMatrixSet(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
Index: Integer;
begin
if ParentOfResult then
AResult.FreeByParentExpression;
AResult := nil;
ParentOfResult := false;
Index := TCasScriptInteger(Arguments[1]).Value;
if not Between(Index, 0, MatrixGetCount - 1) then
raise ECasScriptError.CreateFmt('Invalid index %d for matrix_set on %d-column matrix',
[Index, MatrixGetCount]);
TCasScriptMatrixXx(Arguments[0]).FValue[Index] := TCasScriptVecXx(Arguments[2]).Value;
TCasScriptMatrixXx(Arguments[0]).ValueAssigned := true;
AResult := Arguments[0];
end;
class procedure TCasScriptMatrixXx.HandleMatrixGetCount(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := MatrixGetCount;
end;
procedure TCasScriptMatrixXx.AssignValue(Source: TCasScriptValue);
begin
if Source is TCasScriptMatrixXx then
Value := TCasScriptMatrixXx(Source).Value else
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]);
end;
procedure TCasScriptMatrixXx.SetValue(const AValue: TMatrixXxx);
begin
FValue := AValue;
ValueAssigned := true;
end;
procedure RegisterMatrixXxFunctions;
var
MatrixArgClasses: array of TCasScriptValueClass;
I: Integer;
begin
{ TCasScriptMatrixXx, functions from CastleScriptCoreFunctions }
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleAdd, TCasScriptAdd, [TCasScriptMatrixXx], true);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleSubtract, TCasScriptSubtract, [TCasScriptMatrixXx], true);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleNegate, TCasScriptNegate, [TCasScriptMatrixXx], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMultiply, TCasScriptMultiply, [TCasScriptMatrixXx, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMultiply, TCasScriptMultiply, [TCasScriptFloat, TCasScriptMatrixXx], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMultiply, TCasScriptMultiply, [TCasScriptMatrixXx, TCasScriptVecXx], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMultiply, TCasScriptMultiply, [TCasScriptMatrixXx], true);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleDivide, TCasScriptDivide, [TCasScriptMatrixXx, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleEqual, TCasScriptEqual, [TCasScriptMatrixXx, TCasScriptMatrixXx], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleNotEqual, TCasScriptNotEqual, [TCasScriptMatrixXx, TCasScriptMatrixXx], false);
{ TCasScriptMatrixXx, functions from CastleScriptVectors }
SetLength(MatrixArgClasses, MatrixGetCount);
for I := 0 to MatrixGetCount - 1 do
MatrixArgClasses[I] := TCasScriptVecXx;
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMatrix, TCasScriptMatrixFun, MatrixArgClasses, false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMatrixGet, TCasScriptMatrixGet, [TCasScriptMatrixXx, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMatrixSet, TCasScriptMatrixSet, [TCasScriptMatrixXx, TCasScriptInteger, TCasScriptVecXx], false);
FunctionHandlers.RegisterHandler(@TCasScriptMatrixXx(nil).HandleMatrixGetCount, TCasScriptMatrixGetCount, [TCasScriptMatrixXx], false);
end;
|