Skip to content

Commit

Permalink
OpenGL: 2D CrossProduct
Browse files Browse the repository at this point in the history
  • Loading branch information
SunSerega committed Oct 23, 2023
1 parent 9eea9e4 commit 325d4bc
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 50 deletions.
3 changes: 2 additions & 1 deletion Log/Release.log
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ Packing sample file "Samples/OpenGLABC/Точки на поле/Empty.vert"
Packing sample file "Samples/OpenGLABC/Точки на поле/Mandelbrot.frag"
Packing sample file "Samples/OpenGLABC/Точки на поле/SinglePointToScreen.geom"
Packing sample file "Samples/OpenGLABC/Точки на поле/Волны.frag"
Packing sample file "Samples/OpenGLABC/Точки на поле/Закраска по функции.frag"
Packing sample file "Samples/OpenGLABC/Точки на поле/Минимум расстояний.frag"
Packing sample file "Samples/OpenGLABC/Точки на поле/Спирали.frag"
Packing sample file "Samples/OpenGLABC/Точки на поле/Сумма расстояний.frag"
Packing sample file "Samples/OpenGLABC/Точки на поле/Точки.pas"
Packed 26 sample files
Packed 27 sample files
12 changes: 8 additions & 4 deletions Log/Test.log
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,17 @@ Tester: Compiling "Tests/Exec/CLABC/03#ToString/13#Wrap/06#CLCode/1.pas"
Tester: Compiling: OK
Tester: Compiling "Tests/Exec/CLABC/04#Samples/Игра жизнь/Игра жизнь.pas"
Tester: Compiling: OK
Tester: Compiling "Tests/Exec/GL/Det.pas"
Tester: Compiling "Tests/Exec/GL/Determinant.pas"
Tester: Compiling: OK
Tester: Compiling "Tests/Exec/GL/Mtr.Rotate3D.pas"
Tester: Compiling: OK
Tester: Compiling "Tests/Exec/GL/Mtr.Translate.pas"
Tester: Compiling: OK
Tester: Compiling "Tests/Exec/GL/Random.pas"
Tester: Compiling: OK
Tester: Compiling "Tests/Exec/GL/Vec.Cross.pas"
Tester: Compiling "Tests/Exec/GL/Vec2d.Cross.pas"
Tester: Compiling: OK
Tester: Compiling "Tests/Exec/GL/Vec3d.Cross.pas"
Tester: Compiling: OK
Tester: Compiling "Samples/OpenCL/MatrMlt.pas"
Tester: Compiling: OK
Expand Down Expand Up @@ -393,15 +395,17 @@ Tester: Executing Test[Tests/Exec/CLABC/03#ToString/13#Wrap/06#CLCode/1]
Tester: Done executing
Tester: Executing Test[Tests/Exec/CLABC/04#Samples/Игра жизнь/Игра жизнь]
Tester: Done executing
Tester: Executing Test[Tests/Exec/GL/Det]
Tester: Executing Test[Tests/Exec/GL/Determinant]
Tester: Done executing
Tester: Executing Test[Tests/Exec/GL/Mtr.Rotate3D]
Tester: Done executing
Tester: Executing Test[Tests/Exec/GL/Mtr.Translate]
Tester: Done executing
Tester: Executing Test[Tests/Exec/GL/Random]
Tester: Done executing
Tester: Executing Test[Tests/Exec/GL/Vec.Cross]
Tester: Executing Test[Tests/Exec/GL/Vec2d.Cross]
Tester: Done executing
Tester: Executing Test[Tests/Exec/GL/Vec3d.Cross]
Tester: Done executing
Tester: Executing Test[Samples/OpenCL/MatrMlt]
Tester: Done executing
Expand Down
24 changes: 24 additions & 0 deletions Modules.Packed/OpenGL.pas
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ Vec2b = record
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2b) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2b) := CrossCW(v2, v1);

end;
///
PVec2b = ^Vec2b;
Expand Down Expand Up @@ -249,6 +253,10 @@ Vec2d = record

public function Normalized := self / Length;

public static function CrossCW(v1, v2: Vec2d) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2d) := CrossCW(v2, v1);

end;
///
PVec2d = ^Vec2d;
Expand Down Expand Up @@ -364,6 +372,10 @@ Vec2f = record

public function Normalized := self / single(Length);

public static function CrossCW(v1, v2: Vec2f) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2f) := CrossCW(v2, v1);

end;
///
PVec2f = ^Vec2f;
Expand Down Expand Up @@ -477,6 +489,10 @@ Vec2i = record
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2i) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2i) := CrossCW(v2, v1);

end;
///
PVec2i = ^Vec2i;
Expand Down Expand Up @@ -582,6 +598,10 @@ Vec2i64 = record
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2i64) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2i64) := CrossCW(v2, v1);

end;
///
PVec2i64 = ^Vec2i64;
Expand Down Expand Up @@ -687,6 +707,10 @@ Vec2s = record
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2s) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2s) := CrossCW(v2, v1);

end;
///
PVec2s = ^Vec2s;
Expand Down
24 changes: 24 additions & 0 deletions Packing/Descriptions/OpenGL.predoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ type
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2b) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2b) := CrossCW(v2, v1);

end;
///
PVec2b = ^Vec2b;
Expand Down Expand Up @@ -245,6 +249,10 @@ type

public function Normalized := self / Length;

public static function CrossCW(v1, v2: Vec2d) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2d) := CrossCW(v2, v1);

end;
///
PVec2d = ^Vec2d;
Expand Down Expand Up @@ -360,6 +368,10 @@ type

public function Normalized := self / single(Length);

public static function CrossCW(v1, v2: Vec2f) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2f) := CrossCW(v2, v1);

end;
///
PVec2f = ^Vec2f;
Expand Down Expand Up @@ -473,6 +485,10 @@ type
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2i) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2i) := CrossCW(v2, v1);

end;
///
PVec2i = ^Vec2i;
Expand Down Expand Up @@ -578,6 +594,10 @@ type
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2i64) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2i64) := CrossCW(v2, v1);

end;
///
PVec2i64 = ^Vec2i64;
Expand Down Expand Up @@ -683,6 +703,10 @@ type
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2s) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2s) := CrossCW(v2, v1);

end;
///
PVec2s = ^Vec2s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,20 @@ <h3>Cross</h3>
<p>В математике произведение векторов может вернуть один из двух противоположных друг-другу векторов,
в зависимости от ориентации системы координат. В модуле <code>OpenGL</code> это решено следующим образом:</p>
<ul>
<li><p><code>Vec3d.CrossCW(new Vec3d(1,0,0), new Vec3d(0,1,0)) = new Vec3d(0,0,1)</code>.</p>
<li><p>CW (Clockwise - по часовой стрелке):
<code>Vec3d.CrossCW(new Vec3d(1,0,0), new Vec3d(0,1,0)) = new Vec3d(0,0,1)</code>.</p>
</li>
<li><p><code>Vec3d.CrossCCW(a,b) = -Vec3d.CrossCW(a,b)</code>;</p>
<li><p>CСW (Counter-Clockwise - против часовой стрелки):
<code>Vec3d.CrossCCW(a,b) = -Vec3d.CrossCW(a,b) = Vec3d.CrossCW(b,a)</code>;</p>
</li>
</ul>
<p>Кроме этого, статические методы <code>.Cross[CW,CCW]</code> так же объявленны для 2D векторов.<br />
Для них результат является z-компонентом соответствующего метода для 3D векторов:</p>
<pre><code>Vec2d.CrossCW(a,b) = new Vec3d(0,0,1) * Vec3d.CrossCW(Vec3d(a),Vec3d(b))
</code></pre>
<p>С другом стороны, 2D векторное произведение это определитель матрицы (не важно, по строкам или столбцам):</p>
<pre><code>Vec2d.CrossCW(a,b) = Mtr2d.FromCols(a,b).Determinant
</code></pre>
<h3>Генерация случайных значений</h3>
<p>Статичный метод <code>.Random</code> создаёт новый вектор из случайных значений в заданном диапазоне:</p>
<pre><code>// Вектор будет иметь значения из [0;1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,21 @@ v2.SqrLength.Sqrt.Println; // Обязательно будет 1
В математике произведение векторов может вернуть один из двух противоположных друг-другу векторов,
в зависимости от ориентации системы координат. В модуле `OpenGL` это решено следующим образом:

- `Vec3d.CrossCW(new Vec3d(1,0,0), new Vec3d(0,1,0)) = new Vec3d(0,0,1)`.
- CW (Clockwise - по часовой стрелке):
`Vec3d.CrossCW(new Vec3d(1,0,0), new Vec3d(0,1,0)) = new Vec3d(0,0,1)`.

- `Vec3d.CrossCCW(a,b) = -Vec3d.CrossCW(a,b)`;
- CСW (Counter-Clockwise - против часовой стрелки):
`Vec3d.CrossCCW(a,b) = -Vec3d.CrossCW(a,b) = Vec3d.CrossCW(b,a)`;

Кроме этого, статические методы `.Cross[CW,CCW]` так же объявленны для 2D векторов.\
Для них результат является z-компонентом соответствующего метода для 3D векторов:
```
Vec2d.CrossCW(a,b) = new Vec3d(0,0,1) * Vec3d.CrossCW(Vec3d(a),Vec3d(b))
```
С другом стороны, 2D векторное произведение это определитель матрицы (не важно, по строкам или столбцам):
```
Vec2d.CrossCW(a,b) = Mtr2d.FromCols(a,b).Determinant
```

### Генерация случайных значений

Expand Down
49 changes: 26 additions & 23 deletions Packing/Template/LowLvl/NamedTypeItems.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1662,32 +1662,37 @@ interface
{$endregion Normalized}

{$region Cross}
if (sz=3) and not Name.IsUnsigned then
if (sz in 2..3) and not Name.IsUnsigned then
begin

wr += ' public static function CrossCW(v1, v2: ';
wr += own_tname;
wr += ') :='#10;

wr += ' new ';
wr += own_tname;
wr += '(';
wr.WriteNumbered(sz,
(wr,i)->
begin
wr += 'v1.val';
wr += (i+1) mod sz;
wr += '*v2.val';
wr += (i+2) mod sz;
wr += ' - v2.val';
wr += (i+1) mod sz;
wr += '*v1.val';
wr += (i+2) mod sz;
end,
(wr,i)->(wr += ', '),
0
);
wr += ');'#10;
if sz=2 then
// Vec2d.CrossCW(v1,v2) = new Vec3d(0,0,1) * Vec3d.CrossCW(Vec3d(v1),Vec3d(v2))
wr += ' v1.val0*v2.val1 - v2.val0*v1.val1;'#10 else
begin
wr += ' new ';
wr += own_tname;
wr += '(';
wr.WriteNumbered(sz,
(wr,i)->
begin
wr += 'v1.val';
wr += (i+1) mod sz;
wr += '*v2.val';
wr += (i+2) mod sz;
wr += ' - v2.val';
wr += (i+1) mod sz;
wr += '*v1.val';
wr += (i+2) mod sz;
end,
(wr,i)->(wr += ', '),
0
);
wr += ');'#10;
end;

wr += ' public static function CrossCCW(v1, v2: ';
wr += own_tname;
Expand Down Expand Up @@ -1858,7 +1863,6 @@ interface

foreach var dir in c.Permutations do
begin
var (d1,d2,d3) := dir;

wr += ' public static function RotatePrefix';
foreach var d in dir do
Expand All @@ -1871,7 +1875,7 @@ interface
if not dir.SequenceEqual(c) and not dir.Reverse.SequenceEqual(c) then
begin
wr += ' := RotatePrefix';
var is_only_rotated := c.Cycle.ElementAt(c.IndexOf(d1)+1) = d2;
var is_only_rotated := c.Cycle.ElementAt(c.IndexOf(dir[0])+1) = dir[1];
foreach var d in is_only_rotated?c:c.Reverse do
wr += 'XYZW'[d];
wr += '(radians, u);'#10;
Expand Down Expand Up @@ -1951,7 +1955,6 @@ interface

foreach var dir in c.Permutations do
begin
var (d1,d2,d3) := dir;

wr += ' public static function RotatePostfix';
foreach var d in dir do
Expand Down
24 changes: 24 additions & 0 deletions Packing/Template/LowLvl/OpenGL/Types.Interface.template
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2b) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2b) := CrossCW(v2, v1);

end;
///
PVec2b = ^Vec2b;
Expand Down Expand Up @@ -209,6 +213,10 @@

public function Normalized := self / Length;

public static function CrossCW(v1, v2: Vec2d) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2d) := CrossCW(v2, v1);

end;
///
PVec2d = ^Vec2d;
Expand Down Expand Up @@ -324,6 +332,10 @@

public function Normalized := self / single(Length);

public static function CrossCW(v1, v2: Vec2f) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2f) := CrossCW(v2, v1);

end;
///
PVec2f = ^Vec2f;
Expand Down Expand Up @@ -437,6 +449,10 @@
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2i) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2i) := CrossCW(v2, v1);

end;
///
PVec2i = ^Vec2i;
Expand Down Expand Up @@ -542,6 +558,10 @@
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2i64) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2i64) := CrossCW(v2, v1);

end;
///
PVec2i64 = ^Vec2i64;
Expand Down Expand Up @@ -647,6 +667,10 @@
public function SqrLength := self*self;
public function Length := Sqrt(SqrLength);

public static function CrossCW(v1, v2: Vec2s) :=
v1.val0*v2.val1 - v2.val0*v1.val1;
public static function CrossCCW(v1, v2: Vec2s) := CrossCW(v2, v1);

end;
///
PVec2s = ^Vec2s;
Expand Down
Loading

0 comments on commit 325d4bc

Please sign in to comment.