├── .editorconfig
├── .github
└── workflows
│ └── tests.yml
├── .gitignore
├── GenericVectors
├── GenericVectors.csproj
├── Program.cs
├── TestingHelpers
│ └── AssertExtensions.cs
├── Tests
│ ├── GenericAssertExtensions.cs
│ ├── GenericMathHelper.cs
│ ├── GenericMatrix3x2DoubleTests.cs
│ ├── GenericMatrix3x2SingleTests.cs
│ ├── GenericMatrix4x4DoubleTests.cs
│ ├── GenericMatrix4x4SingleTests.cs
│ ├── GenericPlaneDoubleTests.cs
│ ├── GenericPlaneSingleTests.cs
│ ├── GenericQuaternionDoubleTests.cs
│ ├── GenericQuaternionSingleTests.cs
│ ├── GenericVector2DoubleTests.cs
│ ├── GenericVector2SingleTests.cs
│ ├── GenericVector2Tests.cs
│ ├── GenericVector3Double.cs
│ ├── GenericVector3SingleTests.cs
│ ├── GenericVector4DoubleTests.cs
│ └── GenericVector4SingleTests.cs
└── src
│ ├── Matrix3x2S.cs
│ ├── Matrix3x2_1.cs
│ ├── Matrix4x4S.cs
│ ├── Matrix4x4_1.cs
│ ├── PlaneS.cs
│ ├── Plane_1.cs
│ ├── QuaternionS.cs
│ ├── Quaternion_1.cs
│ ├── SRInternals.cs
│ ├── Vector2S.cs
│ ├── Vector2_1.cs
│ ├── Vector3S.cs
│ ├── Vector3_1.cs
│ ├── Vector4S.cs
│ └── Vector4_1.cs
├── LICENSE.TXT
├── README.md
└── generic-vectors-matrices.sln
/.editorconfig:
--------------------------------------------------------------------------------
1 | # editorconfig.org
2 |
3 | # top-most EditorConfig file
4 | root = true
5 |
6 | # Default settings:
7 | # A newline ending every file
8 | # Use 4 spaces as indentation
9 | [*]
10 | insert_final_newline = true
11 | indent_style = space
12 | indent_size = 4
13 | trim_trailing_whitespace = true
14 |
15 | [project.json]
16 | indent_size = 2
17 |
18 | # Generated code
19 | [*{_AssemblyInfo.cs,.notsupported.cs}]
20 | generated_code = true
21 |
22 | # C# files
23 | [*.cs]
24 | # New line preferences
25 | csharp_new_line_before_open_brace = all
26 | csharp_new_line_before_else = true
27 | csharp_new_line_before_catch = true
28 | csharp_new_line_before_finally = true
29 | csharp_new_line_before_members_in_object_initializers = true
30 | csharp_new_line_before_members_in_anonymous_types = true
31 | csharp_new_line_between_query_expression_clauses = true
32 |
33 | # Indentation preferences
34 | csharp_indent_block_contents = true
35 | csharp_indent_braces = false
36 | csharp_indent_case_contents = true
37 | csharp_indent_case_contents_when_block = true
38 | csharp_indent_switch_labels = true
39 | csharp_indent_labels = one_less_than_current
40 |
41 | # Modifier preferences
42 | csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
43 |
44 | # avoid this. unless absolutely necessary
45 | dotnet_style_qualification_for_field = false:suggestion
46 | dotnet_style_qualification_for_property = false:suggestion
47 | dotnet_style_qualification_for_method = false:suggestion
48 | dotnet_style_qualification_for_event = false:suggestion
49 |
50 | # Types: use keywords instead of BCL types, and permit var only when the type is clear
51 | csharp_style_var_for_built_in_types = false:suggestion
52 | csharp_style_var_when_type_is_apparent = false:none
53 | csharp_style_var_elsewhere = false:suggestion
54 | dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
55 | dotnet_style_predefined_type_for_member_access = true:suggestion
56 |
57 | # name all constant fields using PascalCase
58 | dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
59 | dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
60 | dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
61 | dotnet_naming_symbols.constant_fields.applicable_kinds = field
62 | dotnet_naming_symbols.constant_fields.required_modifiers = const
63 | dotnet_naming_style.pascal_case_style.capitalization = pascal_case
64 |
65 | # static fields should have s_ prefix
66 | dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
67 | dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
68 | dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
69 | dotnet_naming_symbols.static_fields.applicable_kinds = field
70 | dotnet_naming_symbols.static_fields.required_modifiers = static
71 | dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
72 | dotnet_naming_style.static_prefix_style.required_prefix = s_
73 | dotnet_naming_style.static_prefix_style.capitalization = camel_case
74 |
75 | # internal and private fields should be _camelCase
76 | dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
77 | dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
78 | dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
79 | dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
80 | dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
81 | dotnet_naming_style.camel_case_underscore_style.required_prefix = _
82 | dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
83 |
84 | # Code style defaults
85 | csharp_using_directive_placement = outside_namespace:suggestion
86 | dotnet_sort_system_directives_first = true
87 | csharp_prefer_braces = true:silent
88 | csharp_preserve_single_line_blocks = true:none
89 | csharp_preserve_single_line_statements = false:none
90 | csharp_prefer_static_local_function = true:suggestion
91 | csharp_prefer_simple_using_statement = false:none
92 | csharp_style_prefer_switch_expression = true:suggestion
93 |
94 | # Code quality
95 | dotnet_style_readonly_field = true:suggestion
96 | dotnet_code_quality_unused_parameters = non_public:suggestion
97 |
98 | # Expression-level preferences
99 | dotnet_style_object_initializer = true:suggestion
100 | dotnet_style_collection_initializer = true:suggestion
101 | dotnet_style_explicit_tuple_names = true:suggestion
102 | dotnet_style_coalesce_expression = true:suggestion
103 | dotnet_style_null_propagation = true:suggestion
104 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
105 | dotnet_style_prefer_inferred_tuple_names = true:suggestion
106 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
107 | dotnet_style_prefer_auto_properties = true:suggestion
108 | dotnet_style_prefer_conditional_expression_over_assignment = true:silent
109 | dotnet_style_prefer_conditional_expression_over_return = true:silent
110 | csharp_prefer_simple_default_expression = true:suggestion
111 |
112 | # Expression-bodied members
113 | csharp_style_expression_bodied_methods = true:silent
114 | csharp_style_expression_bodied_constructors = true:silent
115 | csharp_style_expression_bodied_operators = true:silent
116 | csharp_style_expression_bodied_properties = true:silent
117 | csharp_style_expression_bodied_indexers = true:silent
118 | csharp_style_expression_bodied_accessors = true:silent
119 | csharp_style_expression_bodied_lambdas = true:silent
120 | csharp_style_expression_bodied_local_functions = true:silent
121 |
122 | # Pattern matching
123 | csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
124 | csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
125 | csharp_style_inlined_variable_declaration = true:suggestion
126 |
127 | # Null checking preferences
128 | csharp_style_throw_expression = true:suggestion
129 | csharp_style_conditional_delegate_call = true:suggestion
130 |
131 | # Other features
132 | csharp_style_prefer_index_operator = false:none
133 | csharp_style_prefer_range_operator = false:none
134 | csharp_style_pattern_local_over_anonymous_function = false:none
135 |
136 | # Space preferences
137 | csharp_space_after_cast = false
138 | csharp_space_after_colon_in_inheritance_clause = true
139 | csharp_space_after_comma = true
140 | csharp_space_after_dot = false
141 | csharp_space_after_keywords_in_control_flow_statements = true
142 | csharp_space_after_semicolon_in_for_statement = true
143 | csharp_space_around_binary_operators = before_and_after
144 | csharp_space_around_declaration_statements = do_not_ignore
145 | csharp_space_before_colon_in_inheritance_clause = true
146 | csharp_space_before_comma = false
147 | csharp_space_before_dot = false
148 | csharp_space_before_open_square_brackets = false
149 | csharp_space_before_semicolon_in_for_statement = false
150 | csharp_space_between_empty_square_brackets = false
151 | csharp_space_between_method_call_empty_parameter_list_parentheses = false
152 | csharp_space_between_method_call_name_and_opening_parenthesis = false
153 | csharp_space_between_method_call_parameter_list_parentheses = false
154 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
155 | csharp_space_between_method_declaration_name_and_open_parenthesis = false
156 | csharp_space_between_method_declaration_parameter_list_parentheses = false
157 | csharp_space_between_parentheses = false
158 | csharp_space_between_square_brackets = false
159 |
160 | # Analyzers
161 | dotnet_code_quality.CA1052.api_surface = private, internal
162 | dotnet_code_quality.CA1802.api_surface = private, internal
163 | dotnet_code_quality.CA1822.api_surface = private, internal
164 | dotnet_code_quality.CA2208.api_surface = public
165 |
166 | # License header
167 | file_header_template = Licensed to the .NET Foundation under one or more agreements.\nThe .NET Foundation licenses this file to you under the MIT license.
168 |
169 | # C++ Files
170 | [*.{cpp,h,in}]
171 | curly_bracket_next_line = true
172 | indent_brace_style = Allman
173 |
174 | # Xml project files
175 | [*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
176 | indent_size = 2
177 |
178 | [*.{csproj,vbproj,proj,nativeproj,locproj}]
179 | charset = utf-8
180 |
181 | # Xml build files
182 | [*.builds]
183 | indent_size = 2
184 |
185 | # Xml files
186 | [*.{xml,stylecop,resx,ruleset}]
187 | indent_size = 2
188 |
189 | # Xml config files
190 | [*.{props,targets,config,nuspec}]
191 | indent_size = 2
192 |
193 | # YAML config files
194 | [*.{yml,yaml}]
195 | indent_size = 2
196 |
197 | # Shell scripts
198 | [*.sh]
199 | end_of_line = lf
200 | [*.{cmd,bat}]
201 | end_of_line = crlf
--------------------------------------------------------------------------------
/.github/workflows/tests.yml:
--------------------------------------------------------------------------------
1 | name: .NET
2 |
3 | on:
4 | push:
5 | branches: [ main ]
6 | pull_request:
7 | branches: [ main ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: windows-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 | - name: Setup .NET
17 | uses: actions/setup-dotnet@v1
18 | with:
19 | dotnet-version: '7.0.x'
20 | include-prerelease: true
21 | - name: Restore dependencies
22 | run: dotnet restore
23 | - name: Build
24 | run: dotnet build --no-restore
25 | - name: Test
26 | run: dotnet test --no-build --verbosity normal
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Oo]ut/
33 | [Ll]og/
34 | [Ll]ogs/
35 |
36 | # Visual Studio 2015/2017 cache/options directory
37 | .vs/
38 | # Uncomment if you have tasks that create the project's static files in wwwroot
39 | #wwwroot/
40 |
41 | # Visual Studio 2017 auto generated files
42 | Generated\ Files/
43 |
44 | # MSTest test Results
45 | [Tt]est[Rr]esult*/
46 | [Bb]uild[Ll]og.*
47 |
48 | # NUnit
49 | *.VisualState.xml
50 | TestResult.xml
51 | nunit-*.xml
52 |
53 | # Build Results of an ATL Project
54 | [Dd]ebugPS/
55 | [Rr]eleasePS/
56 | dlldata.c
57 |
58 | # Benchmark Results
59 | BenchmarkDotNet.Artifacts/
60 |
61 | # .NET Core
62 | project.lock.json
63 | project.fragment.lock.json
64 | artifacts/
65 |
66 | # ASP.NET Scaffolding
67 | ScaffoldingReadMe.txt
68 |
69 | # StyleCop
70 | StyleCopReport.xml
71 |
72 | # Files built by Visual Studio
73 | *_i.c
74 | *_p.c
75 | *_h.h
76 | *.ilk
77 | *.meta
78 | *.obj
79 | *.iobj
80 | *.pch
81 | *.pdb
82 | *.ipdb
83 | *.pgc
84 | *.pgd
85 | *.rsp
86 | *.sbr
87 | *.tlb
88 | *.tli
89 | *.tlh
90 | *.tmp
91 | *.tmp_proj
92 | *_wpftmp.csproj
93 | *.log
94 | *.vspscc
95 | *.vssscc
96 | .builds
97 | *.pidb
98 | *.svclog
99 | *.scc
100 |
101 | # Chutzpah Test files
102 | _Chutzpah*
103 |
104 | # Visual C++ cache files
105 | ipch/
106 | *.aps
107 | *.ncb
108 | *.opendb
109 | *.opensdf
110 | *.sdf
111 | *.cachefile
112 | *.VC.db
113 | *.VC.VC.opendb
114 |
115 | # Visual Studio profiler
116 | *.psess
117 | *.vsp
118 | *.vspx
119 | *.sap
120 |
121 | # Visual Studio Trace Files
122 | *.e2e
123 |
124 | # TFS 2012 Local Workspace
125 | $tf/
126 |
127 | # Guidance Automation Toolkit
128 | *.gpState
129 |
130 | # ReSharper is a .NET coding add-in
131 | _ReSharper*/
132 | *.[Rr]e[Ss]harper
133 | *.DotSettings.user
134 |
135 | # TeamCity is a build add-in
136 | _TeamCity*
137 |
138 | # DotCover is a Code Coverage Tool
139 | *.dotCover
140 |
141 | # AxoCover is a Code Coverage Tool
142 | .axoCover/*
143 | !.axoCover/settings.json
144 |
145 | # Coverlet is a free, cross platform Code Coverage Tool
146 | coverage*.json
147 | coverage*.xml
148 | coverage*.info
149 |
150 | # Visual Studio code coverage results
151 | *.coverage
152 | *.coveragexml
153 |
154 | # NCrunch
155 | _NCrunch_*
156 | .*crunch*.local.xml
157 | nCrunchTemp_*
158 |
159 | # MightyMoose
160 | *.mm.*
161 | AutoTest.Net/
162 |
163 | # Web workbench (sass)
164 | .sass-cache/
165 |
166 | # Installshield output folder
167 | [Ee]xpress/
168 |
169 | # DocProject is a documentation generator add-in
170 | DocProject/buildhelp/
171 | DocProject/Help/*.HxT
172 | DocProject/Help/*.HxC
173 | DocProject/Help/*.hhc
174 | DocProject/Help/*.hhk
175 | DocProject/Help/*.hhp
176 | DocProject/Help/Html2
177 | DocProject/Help/html
178 |
179 | # Click-Once directory
180 | publish/
181 |
182 | # Publish Web Output
183 | *.[Pp]ublish.xml
184 | *.azurePubxml
185 | # Note: Comment the next line if you want to checkin your web deploy settings,
186 | # but database connection strings (with potential passwords) will be unencrypted
187 | *.pubxml
188 | *.publishproj
189 |
190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
191 | # checkin your Azure Web App publish settings, but sensitive information contained
192 | # in these scripts will be unencrypted
193 | PublishScripts/
194 |
195 | # NuGet Packages
196 | *.nupkg
197 | # NuGet Symbol Packages
198 | *.snupkg
199 | # The packages folder can be ignored because of Package Restore
200 | **/[Pp]ackages/*
201 | # except build/, which is used as an MSBuild target.
202 | !**/[Pp]ackages/build/
203 | # Uncomment if necessary however generally it will be regenerated when needed
204 | #!**/[Pp]ackages/repositories.config
205 | # NuGet v3's project.json files produces more ignorable files
206 | *.nuget.props
207 | *.nuget.targets
208 |
209 | # Microsoft Azure Build Output
210 | csx/
211 | *.build.csdef
212 |
213 | # Microsoft Azure Emulator
214 | ecf/
215 | rcf/
216 |
217 | # Windows Store app package directories and files
218 | AppPackages/
219 | BundleArtifacts/
220 | Package.StoreAssociation.xml
221 | _pkginfo.txt
222 | *.appx
223 | *.appxbundle
224 | *.appxupload
225 |
226 | # Visual Studio cache files
227 | # files ending in .cache can be ignored
228 | *.[Cc]ache
229 | # but keep track of directories ending in .cache
230 | !?*.[Cc]ache/
231 |
232 | # Others
233 | ClientBin/
234 | ~$*
235 | *~
236 | *.dbmdl
237 | *.dbproj.schemaview
238 | *.jfm
239 | *.pfx
240 | *.publishsettings
241 | orleans.codegen.cs
242 |
243 | # Including strong name files can present a security risk
244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
245 | #*.snk
246 |
247 | # Since there are multiple workflows, uncomment next line to ignore bower_components
248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
249 | #bower_components/
250 |
251 | # RIA/Silverlight projects
252 | Generated_Code/
253 |
254 | # Backup & report files from converting an old project file
255 | # to a newer Visual Studio version. Backup files are not needed,
256 | # because we have git ;-)
257 | _UpgradeReport_Files/
258 | Backup*/
259 | UpgradeLog*.XML
260 | UpgradeLog*.htm
261 | ServiceFabricBackup/
262 | *.rptproj.bak
263 |
264 | # SQL Server files
265 | *.mdf
266 | *.ldf
267 | *.ndf
268 |
269 | # Business Intelligence projects
270 | *.rdl.data
271 | *.bim.layout
272 | *.bim_*.settings
273 | *.rptproj.rsuser
274 | *- [Bb]ackup.rdl
275 | *- [Bb]ackup ([0-9]).rdl
276 | *- [Bb]ackup ([0-9][0-9]).rdl
277 |
278 | # Microsoft Fakes
279 | FakesAssemblies/
280 |
281 | # GhostDoc plugin setting file
282 | *.GhostDoc.xml
283 |
284 | # Node.js Tools for Visual Studio
285 | .ntvs_analysis.dat
286 | node_modules/
287 |
288 | # Visual Studio 6 build log
289 | *.plg
290 |
291 | # Visual Studio 6 workspace options file
292 | *.opt
293 |
294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
295 | *.vbw
296 |
297 | # Visual Studio LightSwitch build output
298 | **/*.HTMLClient/GeneratedArtifacts
299 | **/*.DesktopClient/GeneratedArtifacts
300 | **/*.DesktopClient/ModelManifest.xml
301 | **/*.Server/GeneratedArtifacts
302 | **/*.Server/ModelManifest.xml
303 | _Pvt_Extensions
304 |
305 | # Paket dependency manager
306 | .paket/paket.exe
307 | paket-files/
308 |
309 | # FAKE - F# Make
310 | .fake/
311 |
312 | # CodeRush personal settings
313 | .cr/personal
314 |
315 | # Python Tools for Visual Studio (PTVS)
316 | __pycache__/
317 | *.pyc
318 |
319 | # Cake - Uncomment if you are using it
320 | # tools/**
321 | # !tools/packages.config
322 |
323 | # Tabs Studio
324 | *.tss
325 |
326 | # Telerik's JustMock configuration file
327 | *.jmconfig
328 |
329 | # BizTalk build output
330 | *.btp.cs
331 | *.btm.cs
332 | *.odx.cs
333 | *.xsd.cs
334 |
335 | # OpenCover UI analysis results
336 | OpenCover/
337 |
338 | # Azure Stream Analytics local run output
339 | ASALocalRun/
340 |
341 | # MSBuild Binary and Structured Log
342 | *.binlog
343 |
344 | # NVidia Nsight GPU debugger configuration file
345 | *.nvuser
346 |
347 | # MFractors (Xamarin productivity tool) working folder
348 | .mfractor/
349 |
350 | # Local History for Visual Studio
351 | .localhistory/
352 |
353 | # BeatPulse healthcheck temp database
354 | healthchecksdb
355 |
356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
357 | MigrationBackup/
358 |
359 | # Ionide (cross platform F# VS Code tools) working folder
360 | .ionide/
361 |
362 | # Fody - auto-generated XML schema
363 | FodyWeavers.xsd
--------------------------------------------------------------------------------
/GenericVectors/GenericVectors.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net7.0
6 | generic_vectors_matrices
7 | enable
8 | AllEnabledByDefault
9 | 11
10 | true
11 | true
12 |
13 |
14 |
15 | True
16 |
17 |
18 |
19 | True
20 |
21 |
22 |
23 |
24 |
25 |
26 | runtime; build; native; contentfiles; analyzers; buildtransitive
27 | all
28 |
29 |
30 | runtime; build; native; contentfiles; analyzers; buildtransitive
31 | all
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/GenericVectors/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Numerics;
3 |
4 | var transform = Matrix3x2S.CreateTranslation(-100.0, -100.0);
5 | transform *= Matrix3x2S.CreateScale(2.0, 2.0);
6 | transform *= Matrix3x2S.CreateTranslation(100.0, 100.0);
7 |
8 | var point = new Vector2(105, 110);
9 | var transformed = Vector2S.Transform(point, transform);
10 |
11 | // <105, 110> => <110, 120>
12 | Console.Write($"{point} => {transformed}");
13 |
--------------------------------------------------------------------------------
/GenericVectors/Tests/GenericAssertExtensions.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | using System.Numerics;
5 | using Xunit;
6 |
7 | namespace System
8 | {
9 | public static class GenericAssertExtensions
10 | {
11 | private const int Precision = 5;
12 |
13 | public static void Equal(Vector2 expected, Vector2 actual)
14 | {
15 | Assert.Equal((float)expected.X, (float)actual.X, Precision);
16 | Assert.Equal((float)expected.Y, (float)actual.Y, Precision);
17 | }
18 |
19 | public static void Equal(Vector2 expected, Vector2 actual)
20 | {
21 | Assert.Equal(expected.X, actual.X, Precision);
22 | Assert.Equal(expected.Y, actual.Y, Precision);
23 | }
24 |
25 | public static void Equal(Vector2 expected, Vector2 actual)
26 | {
27 | Assert.Equal(expected.X, actual.X, Precision);
28 | Assert.Equal(expected.Y, actual.Y, Precision);
29 | }
30 |
31 | public static void Equal(Vector3 expected, Vector3 actual)
32 | {
33 | Assert.Equal((float)expected.X, (float)actual.X, Precision);
34 | Assert.Equal((float)expected.Y, (float)actual.Y, Precision);
35 | Assert.Equal((float)expected.Z, (float)actual.Z, Precision);
36 | }
37 |
38 | public static void Equal(Vector3 expected, Vector3 actual)
39 | {
40 | Assert.Equal(expected.X, actual.X, Precision);
41 | Assert.Equal(expected.Y, actual.Y, Precision);
42 | Assert.Equal(expected.Z, actual.Z, Precision);
43 | }
44 |
45 | public static void Equal(Vector3 expected, Vector3 actual)
46 | {
47 | Assert.Equal(expected.X, actual.X, Precision);
48 | Assert.Equal(expected.Y, actual.Y, Precision);
49 | Assert.Equal(expected.Z, actual.Z, Precision);
50 | }
51 |
52 | public static void Equal(Vector4 expected, Vector4 actual)
53 | {
54 | Assert.Equal((float)expected.X, (float)actual.X, Precision);
55 | Assert.Equal((float)expected.Y, (float)actual.Y, Precision);
56 | Assert.Equal((float)expected.Z, (float)actual.Z, Precision);
57 | Assert.Equal((float)expected.W, (float)actual.W, Precision);
58 | }
59 |
60 | public static void Equal(Vector4 expected, Vector4 actual)
61 | {
62 | Assert.Equal(expected.X, actual.X, Precision);
63 | Assert.Equal(expected.Y, actual.Y, Precision);
64 | Assert.Equal(expected.Z, actual.Z, Precision);
65 | Assert.Equal(expected.W, actual.W, Precision);
66 | }
67 |
68 | public static void Equal(Vector4 expected, Vector4 actual)
69 | {
70 | Assert.Equal(expected.X, actual.X, Precision);
71 | Assert.Equal(expected.Y, actual.Y, Precision);
72 | Assert.Equal(expected.Z, actual.Z, Precision);
73 | Assert.Equal(expected.W, actual.W, Precision);
74 | }
75 |
76 | public static void Equal(Quaternion expected, Quaternion actual)
77 | {
78 | Assert.Equal((float)expected.X, (float)actual.X, Precision);
79 | Assert.Equal((float)expected.Y, (float)actual.Y, Precision);
80 | Assert.Equal((float)expected.Z, (float)actual.Z, Precision);
81 | Assert.Equal((float)expected.W, (float)actual.W, Precision);
82 | }
83 |
84 | public static void Equal(Quaternion expected, Quaternion actual)
85 | {
86 | Assert.Equal(expected.X, actual.X, Precision);
87 | Assert.Equal(expected.Y, actual.Y, Precision);
88 | Assert.Equal(expected.Z, actual.Z, Precision);
89 | Assert.Equal(expected.W, actual.W, Precision);
90 | }
91 |
92 | public static void Equal(Quaternion expected, Quaternion actual)
93 | {
94 | Assert.Equal(expected.X, actual.X, Precision);
95 | Assert.Equal(expected.Y, actual.Y, Precision);
96 | Assert.Equal(expected.Z, actual.Z, Precision);
97 | Assert.Equal(expected.W, actual.W, Precision);
98 | }
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/GenericVectors/Tests/GenericMathHelper.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | namespace System.Numerics.Tests
5 | {
6 | static class GenericMathHelper
7 | {
8 | public const float Pi = MathF.PI;
9 | public const float PiOver2 = Pi / 2f;
10 | public const float PiOver4 = Pi / 4f;
11 |
12 | // Angle conversion helper.
13 | public static T ToRadians(T degrees)
14 | where T : struct, IFloatingPointIeee754
15 | {
16 | return degrees * T.CreateChecked(Math.PI / 180.0);
17 | }
18 |
19 | // Comparison helpers with small tolerance to allow for floating point rounding during computations.
20 | public static bool Equal(T a, T b)
21 | where T : struct, IFloatingPointIeee754
22 | {
23 | return (T.Abs(a - b) < T.CreateChecked(1e-5));
24 | }
25 |
26 | public static bool Equal(Vector2 a, Vector2 b)
27 | where T : struct, IFloatingPointIeee754
28 | {
29 | return Equal(a.X, b.X) && Equal(a.Y, b.Y);
30 | }
31 |
32 | public static bool Equal(Vector3 a, Vector3 b)
33 | where T : struct, IFloatingPointIeee754
34 | {
35 | return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z);
36 | }
37 |
38 | public static bool Equal(Vector4 a, Vector4 b)
39 | where T : struct, IFloatingPointIeee754
40 | {
41 | return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z) && Equal(a.W, b.W); ;
42 | }
43 |
44 | public static bool Equal(Matrix4x4 a, Matrix4x4 b)
45 | where T : struct, IFloatingPointIeee754
46 | {
47 | return
48 | Equal(a.M11, b.M11) && Equal(a.M12, b.M12) && Equal(a.M13, b.M13) && Equal(a.M14, b.M14) &&
49 | Equal(a.M21, b.M21) && Equal(a.M22, b.M22) && Equal(a.M23, b.M23) && Equal(a.M24, b.M24) &&
50 | Equal(a.M31, b.M31) && Equal(a.M32, b.M32) && Equal(a.M33, b.M33) && Equal(a.M34, b.M34) &&
51 | Equal(a.M41, b.M41) && Equal(a.M42, b.M42) && Equal(a.M43, b.M43) && Equal(a.M44, b.M44);
52 | }
53 |
54 | public static bool Equal(Matrix3x2 a, Matrix3x2 b)
55 | where T : struct, IFloatingPointIeee754
56 | {
57 | return
58 | Equal(a.M11, b.M11) && Equal(a.M12, b.M12) &&
59 | Equal(a.M21, b.M21) && Equal(a.M22, b.M22) &&
60 | Equal(a.M31, b.M31) && Equal(a.M32, b.M32);
61 | }
62 |
63 | public static bool Equal(Plane a, Plane b)
64 | where T : struct, IFloatingPointIeee754
65 | {
66 | return Equal(a.Normal, b.Normal) && Equal(a.Distance, b.Distance);
67 | }
68 |
69 | public static bool Equal(Quaternion a, Quaternion b)
70 | where T : struct, IFloatingPointIeee754
71 | {
72 | return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z) && Equal(a.W, b.W);
73 | }
74 |
75 | public static bool EqualRotation(Quaternion a, Quaternion b)
76 | where T : struct, IFloatingPointIeee754
77 | {
78 | return Equal(a, b) || Equal(a, -b);
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/GenericVectors/Tests/GenericPlaneDoubleTests.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | using System.Globalization;
5 | using System.Runtime.InteropServices;
6 | using Xunit;
7 | using GMatrix4x4 = System.Numerics.Matrix4x4;
8 | using GPlane = System.Numerics.Plane;
9 | using GQuaternion = System.Numerics.Quaternion;
10 | using GScalar = System.Double;
11 | using GVector3 = System.Numerics.Vector3;
12 | using GVector4 = System.Numerics.Vector4;
13 |
14 | namespace System.Numerics.Tests
15 | {
16 | public class GenericPlaneDoubleTests
17 | {
18 | // A test for Equals (Plane)
19 | [Fact]
20 | public void PlaneEqualsTest1()
21 | {
22 | GPlane a = new GPlane(1, 2, 3, 4);
23 | GPlane b = new GPlane(1, 2, 3, 4);
24 |
25 | // case 1: compare between same values
26 | bool expected = true;
27 | bool actual = a.Equals(b);
28 | Assert.Equal(expected, actual);
29 |
30 | // case 2: compare between different values
31 | b = b with { Normal = new GVector3(10, b.Normal.Y, b.Normal.Z) };
32 | expected = false;
33 | actual = a.Equals(b);
34 | Assert.Equal(expected, actual);
35 | }
36 |
37 | // A test for Equals (object)
38 | [Fact]
39 | public void PlaneEqualsTest()
40 | {
41 | GPlane a = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
42 | GPlane b = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
43 |
44 | // case 1: compare between same values
45 | object? obj = b;
46 |
47 | bool expected = true;
48 | bool actual = a.Equals(obj);
49 | Assert.Equal(expected, actual);
50 |
51 | // case 2: compare between different values
52 | b = b with { Normal = new GVector3(10.0f, b.Normal.Y, b.Normal.Z) };
53 |
54 | obj = b;
55 | expected = false;
56 | actual = a.Equals(obj);
57 | Assert.Equal(expected, actual);
58 |
59 | // case 3: compare between different types.
60 | obj = new GQuaternion();
61 | expected = false;
62 | actual = a.Equals(obj);
63 | Assert.Equal(expected, actual);
64 |
65 | // case 3: compare against null.
66 | obj = null;
67 | expected = false;
68 | #pragma warning disable CA1508 // Avoid dead conditional code
69 | actual = a.Equals(obj);
70 | #pragma warning restore CA1508 // Avoid dead conditional code
71 | Assert.Equal(expected, actual);
72 | }
73 |
74 | // A test for operator != (Plane, Plane)
75 | [Fact]
76 | public void PlaneInequalityTest()
77 | {
78 | GPlane a = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
79 | GPlane b = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
80 |
81 | // case 1: compare between same values
82 | bool expected = false;
83 | bool actual = a != b;
84 | Assert.Equal(expected, actual);
85 |
86 | // case 2: compare between different values
87 | b = b with { Normal = new GVector3(10.0f, b.Normal.Y, b.Normal.Z) };
88 | expected = true;
89 | actual = a != b;
90 | Assert.Equal(expected, actual);
91 | }
92 |
93 | // A test for operator == (Plane, Plane)
94 | [Fact]
95 | public void PlaneEqualityTest()
96 | {
97 | GPlane a = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
98 | GPlane b = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
99 |
100 | // case 1: compare between same values
101 | bool expected = true;
102 | bool actual = a == b;
103 | Assert.Equal(expected, actual);
104 |
105 | // case 2: compare between different values
106 | b = b with { Normal = new GVector3(10.0f, b.Normal.Y, b.Normal.Z) };
107 | expected = false;
108 | actual = a == b;
109 | Assert.Equal(expected, actual);
110 | }
111 |
112 | // A test for Plane (scalar, scalar, scalar, scalar)
113 | [Fact]
114 | public void PlaneConstructorTest1()
115 | {
116 | GScalar a = 1.0f, b = 2.0f, c = 3.0f, d = 4.0f;
117 | GPlane target = new GPlane(a, b, c, d);
118 |
119 | Assert.True(
120 | target.Normal.X == a && target.Normal.Y == b && target.Normal.Z == c && target.Distance == d,
121 | "GPlane.cstor did not return the expected value.");
122 | }
123 |
124 | // A test for Plane.CreateFromVertices
125 | [Fact]
126 | public void PlaneCreateFromVerticesTest()
127 | {
128 | GVector3 point1 = new GVector3(0, 1, 1);
129 | GVector3 point2 = new GVector3(0, 0, 1);
130 | GVector3 point3 = new GVector3(1, 0, 1);
131 |
132 | GPlane target = PlaneS.CreateFromVertices(point1, point2, point3);
133 | GPlane expected = new GPlane(new GVector3(0, 0, 1), -1);
134 | Assert.Equal(target, expected);
135 | }
136 |
137 | // A test for Plane.CreateFromVertices
138 | [Fact]
139 | public void PlaneCreateFromVerticesTest2()
140 | {
141 | GVector3 point1 = new GVector3(0, 0, 1);
142 | GVector3 point2 = new GVector3(1, 0, 0);
143 | GVector3 point3 = new GVector3(1, 1, 0);
144 |
145 | GPlane target = PlaneS.CreateFromVertices(point1, point2, point3);
146 | GScalar invRoot2 = (GScalar)(1.0 / Math.Sqrt(2));
147 |
148 | GPlane expected = new GPlane(new GVector3(invRoot2, 0, invRoot2), -invRoot2);
149 | Assert.True(GenericMathHelper.Equal(target, expected), "GPlane.cstor did not return the expected value.");
150 | }
151 |
152 | // A test for Plane (Vector3, scalar)
153 | [Fact]
154 | public void PlaneConstructorTest3()
155 | {
156 | GVector3 normal = new GVector3(1, 2, 3);
157 | GScalar d = 4;
158 |
159 | GPlane target = new GPlane(normal, d);
160 | Assert.True(
161 | target.Normal == normal && target.Distance == d,
162 | "GPlane.cstor did not return the expected value.");
163 | }
164 |
165 | // A test for GPlane (Vector4)
166 | [Fact]
167 | public void PlaneConstructorTest()
168 | {
169 | GVector4 value = new GVector4(1, 2, 3, 4);
170 | GPlane target = new GPlane(value);
171 |
172 | Assert.True(
173 | target.Normal.X == value.X && target.Normal.Y == value.Y && target.Normal.Z == value.Z && target.Distance == value.W,
174 | "GPlane.cstor did not return the expected value.");
175 | }
176 |
177 | [Fact]
178 | public void PlaneDotTest()
179 | {
180 | GPlane target = new GPlane(2, 3, 4, 5);
181 | GVector4 value = new GVector4(5, 4, 3, 2);
182 |
183 | GScalar expected = 10 + 12 + 12 + 10;
184 | GScalar actual = PlaneS.Dot(target, value);
185 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Dot returns unexpected value.");
186 | }
187 |
188 | [Fact]
189 | public void PlaneDotCoordinateTest()
190 | {
191 | GPlane target = new GPlane(2, 3, 4, 5);
192 | GVector3 value = new GVector3(5, 4, 3);
193 |
194 | GScalar expected = 10 + 12 + 12 + 5;
195 | GScalar actual = PlaneS.DotCoordinate(target, value);
196 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.DotCoordinate returns unexpected value.");
197 | }
198 |
199 | [Fact]
200 | public void PlaneDotNormalTest()
201 | {
202 | GPlane target = new GPlane(2, 3, 4, 5);
203 | GVector3 value = new GVector3(5, 4, 3);
204 |
205 | GScalar expected = 10 + 12 + 12;
206 | GScalar actual = PlaneS.DotNormal(target, value);
207 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.DotCoordinate returns unexpected value.");
208 | }
209 |
210 | [Fact]
211 | public void PlaneNormalizeTest()
212 | {
213 | GPlane target = new GPlane(1, 2, 3, 4);
214 |
215 | GScalar f = target.Normal.LengthSquared();
216 | GScalar invF = (GScalar)(1.0 / Math.Sqrt(f));
217 | GPlane expected = new GPlane(target.Normal * invF, target.Distance * invF);
218 |
219 | GPlane actual = PlaneS.Normalize(target);
220 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Normalize returns unexpected value.");
221 |
222 | // normalize, normalized normal.
223 | actual = PlaneS.Normalize(actual);
224 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Normalize returns unexpected value.");
225 | }
226 |
227 | [Fact]
228 | // Transform by matrix
229 | public void PlaneTransformTest1()
230 | {
231 | GPlane target = new GPlane(1, 2, 3, 4);
232 | target = PlaneS.Normalize(target);
233 |
234 | GMatrix4x4 m =
235 | Matrix4x4S.CreateRotationX(GenericMathHelper.ToRadians((GScalar)30)) *
236 | Matrix4x4S.CreateRotationY(GenericMathHelper.ToRadians((GScalar)30)) *
237 | Matrix4x4S.CreateRotationZ(GenericMathHelper.ToRadians((GScalar)30));
238 | m = m with { M41 = 10, M42 = 20, M43 = 30 };
239 |
240 | GPlane expected = new GPlane();
241 | GMatrix4x4 inv;
242 | Matrix4x4S.Invert(m, out inv);
243 | GMatrix4x4 itm = Matrix4x4S.Transpose(inv);
244 | GScalar x = target.Normal.X, y = target.Normal.Y, z = target.Normal.Z, w = target.Distance;
245 | expected = expected with
246 | {
247 | Normal = new GVector3(
248 | x * itm.M11 + y * itm.M21 + z * itm.M31 + w * itm.M41,
249 | x * itm.M12 + y * itm.M22 + z * itm.M32 + w * itm.M42,
250 | x * itm.M13 + y * itm.M23 + z * itm.M33 + w * itm.M43)
251 | };
252 | expected = expected with { Distance = x * itm.M14 + y * itm.M24 + z * itm.M34 + w * itm.M44 };
253 |
254 | GPlane actual;
255 | actual = PlaneS.Transform(target, m);
256 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Transform did not return the expected value.");
257 | }
258 |
259 | [Fact]
260 | // Transform by quaternion
261 | public void PlaneTransformTest2()
262 | {
263 | GPlane target = new GPlane(1, 2, 3, 4);
264 | target = PlaneS.Normalize(target);
265 |
266 | GMatrix4x4 m =
267 | Matrix4x4S.CreateRotationX(GenericMathHelper.ToRadians((GScalar)30)) *
268 | Matrix4x4S.CreateRotationY(GenericMathHelper.ToRadians((GScalar)30)) *
269 | Matrix4x4S.CreateRotationZ(GenericMathHelper.ToRadians((GScalar)30));
270 | GQuaternion q = QuaternionS.CreateFromRotationMatrix(m);
271 |
272 | GPlane expected = new GPlane();
273 | GScalar x = target.Normal.X, y = target.Normal.Y, z = target.Normal.Z, w = target.Distance;
274 | expected = expected with
275 | {
276 | Normal = new GVector3(
277 | x * m.M11 + y * m.M21 + z * m.M31 + w * m.M41,
278 | x * m.M12 + y * m.M22 + z * m.M32 + w * m.M42,
279 | x * m.M13 + y * m.M23 + z * m.M33 + w * m.M43)
280 | };
281 | expected = expected with { Distance = x * m.M14 + y * m.M24 + z * m.M34 + w * m.M44 };
282 |
283 | GPlane actual;
284 | actual = PlaneS.Transform(target, q);
285 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Transform did not return the expected value.");
286 | }
287 |
288 | // A test for Plane comparison involving NaN values
289 | [Fact]
290 | public void PlaneEqualsNanTest()
291 | {
292 | GPlane a = new GPlane(GScalar.NaN, 0, 0, 0);
293 | GPlane b = new GPlane(0, GScalar.NaN, 0, 0);
294 | GPlane c = new GPlane(0, 0, GScalar.NaN, 0);
295 | GPlane d = new GPlane(0, 0, 0, GScalar.NaN);
296 |
297 | Assert.False(a == new GPlane(0, 0, 0, 0));
298 | Assert.False(b == new GPlane(0, 0, 0, 0));
299 | Assert.False(c == new GPlane(0, 0, 0, 0));
300 | Assert.False(d == new GPlane(0, 0, 0, 0));
301 |
302 | Assert.True(a != new GPlane(0, 0, 0, 0));
303 | Assert.True(b != new GPlane(0, 0, 0, 0));
304 | Assert.True(c != new GPlane(0, 0, 0, 0));
305 | Assert.True(d != new GPlane(0, 0, 0, 0));
306 |
307 | Assert.False(a.Equals(new GPlane(0, 0, 0, 0)));
308 | Assert.False(b.Equals(new GPlane(0, 0, 0, 0)));
309 | Assert.False(c.Equals(new GPlane(0, 0, 0, 0)));
310 | Assert.False(d.Equals(new GPlane(0, 0, 0, 0)));
311 |
312 | // Counterintuitive result - IEEE rules for NaN comparison are weird!
313 | Assert.False(a.Equals(a));
314 | Assert.False(b.Equals(b));
315 | Assert.False(c.Equals(c));
316 | Assert.False(d.Equals(d));
317 | }
318 |
319 | // A test to make sure these types are blittable directly into GPU buffer memory layouts
320 | [Fact]
321 | public unsafe void PlaneSizeofTest()
322 | {
323 | int scalarSize = sizeof(GScalar);
324 | #pragma warning disable xUnit2000 // Constants and literals should be the expected argument
325 | Assert.Equal(scalarSize * 4, sizeof(GPlane));
326 | Assert.Equal(scalarSize * 8, sizeof(Plane_2x));
327 | Assert.Equal(scalarSize * 5, sizeof(PlanePlusSingle));
328 | Assert.Equal(scalarSize * 10, sizeof(PlanePlusFloatSingle_2x));
329 | #pragma warning restore xUnit2000 // Constants and literals should be the expected argument
330 | }
331 |
332 | [Fact]
333 | public void PlaneToStringTest()
334 | {
335 | GPlane target = new GPlane(1, 2, 3, 4);
336 | string expected = string.Format(
337 | CultureInfo.CurrentCulture,
338 | "{{Normal:{0:G} Distance:{1}}}",
339 | target.Normal,
340 | target.Distance);
341 |
342 | Assert.Equal(expected, target.ToString());
343 | }
344 |
345 | [StructLayout(LayoutKind.Sequential)]
346 | struct Plane_2x
347 | {
348 | private GPlane _a;
349 | private GPlane _b;
350 | }
351 |
352 | [StructLayout(LayoutKind.Sequential)]
353 | struct PlanePlusSingle
354 | {
355 | private GPlane _v;
356 | private GScalar _f;
357 | }
358 |
359 | [StructLayout(LayoutKind.Sequential)]
360 | struct PlanePlusFloatSingle_2x
361 | {
362 | private PlanePlusSingle _a;
363 | private PlanePlusSingle _b;
364 | }
365 | }
366 | }
367 |
--------------------------------------------------------------------------------
/GenericVectors/Tests/GenericPlaneSingleTests.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | using System.Globalization;
5 | using System.Runtime.InteropServices;
6 | using Xunit;
7 | using GMatrix4x4 = System.Numerics.Matrix4x4;
8 | using GPlane = System.Numerics.Plane;
9 | using GQuaternion = System.Numerics.Quaternion;
10 | using GScalar = System.Single;
11 | using GVector3 = System.Numerics.Vector3;
12 | using GVector4 = System.Numerics.Vector4;
13 |
14 | namespace System.Numerics.Tests
15 | {
16 | public class GenericPlaneSingleTests
17 | {
18 | // A test for Equals (Plane)
19 | [Fact]
20 | public void PlaneEqualsTest1()
21 | {
22 | GPlane a = new GPlane(1, 2, 3, 4);
23 | GPlane b = new GPlane(1, 2, 3, 4);
24 |
25 | // case 1: compare between same values
26 | bool expected = true;
27 | bool actual = a.Equals(b);
28 | Assert.Equal(expected, actual);
29 |
30 | // case 2: compare between different values
31 | b = b with { Normal = new GVector3(10, b.Normal.Y, b.Normal.Z) };
32 | expected = false;
33 | actual = a.Equals(b);
34 | Assert.Equal(expected, actual);
35 | }
36 |
37 | // A test for Equals (object)
38 | [Fact]
39 | public void PlaneEqualsTest()
40 | {
41 | GPlane a = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
42 | GPlane b = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
43 |
44 | // case 1: compare between same values
45 | object? obj = b;
46 |
47 | bool expected = true;
48 | bool actual = a.Equals(obj);
49 | Assert.Equal(expected, actual);
50 |
51 | // case 2: compare between different values
52 | b = b with { Normal = new GVector3(10.0f, b.Normal.Y, b.Normal.Z) };
53 |
54 | obj = b;
55 | expected = false;
56 | actual = a.Equals(obj);
57 | Assert.Equal(expected, actual);
58 |
59 | // case 3: compare between different types.
60 | obj = new GQuaternion();
61 | expected = false;
62 | actual = a.Equals(obj);
63 | Assert.Equal(expected, actual);
64 |
65 | // case 3: compare against null.
66 | obj = null;
67 | expected = false;
68 | #pragma warning disable CA1508 // Avoid dead conditional code
69 | actual = a.Equals(obj);
70 | #pragma warning restore CA1508 // Avoid dead conditional code
71 | Assert.Equal(expected, actual);
72 | }
73 |
74 | // A test for operator != (Plane, Plane)
75 | [Fact]
76 | public void PlaneInequalityTest()
77 | {
78 | GPlane a = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
79 | GPlane b = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
80 |
81 | // case 1: compare between same values
82 | bool expected = false;
83 | bool actual = a != b;
84 | Assert.Equal(expected, actual);
85 |
86 | // case 2: compare between different values
87 | b = b with { Normal = new GVector3(10.0f, b.Normal.Y, b.Normal.Z) };
88 | expected = true;
89 | actual = a != b;
90 | Assert.Equal(expected, actual);
91 | }
92 |
93 | // A test for operator == (Plane, Plane)
94 | [Fact]
95 | public void PlaneEqualityTest()
96 | {
97 | GPlane a = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
98 | GPlane b = new GPlane(1.0f, 2.0f, 3.0f, 4.0f);
99 |
100 | // case 1: compare between same values
101 | bool expected = true;
102 | bool actual = a == b;
103 | Assert.Equal(expected, actual);
104 |
105 | // case 2: compare between different values
106 | b = b with { Normal = new GVector3(10.0f, b.Normal.Y, b.Normal.Z) };
107 | expected = false;
108 | actual = a == b;
109 | Assert.Equal(expected, actual);
110 | }
111 |
112 | // A test for Plane (scalar, scalar, scalar, scalar)
113 | [Fact]
114 | public void PlaneConstructorTest1()
115 | {
116 | GScalar a = 1.0f, b = 2.0f, c = 3.0f, d = 4.0f;
117 | GPlane target = new GPlane(a, b, c, d);
118 |
119 | Assert.True(
120 | target.Normal.X == a && target.Normal.Y == b && target.Normal.Z == c && target.Distance == d,
121 | "GPlane.cstor did not return the expected value.");
122 | }
123 |
124 | // A test for Plane.CreateFromVertices
125 | [Fact]
126 | public void PlaneCreateFromVerticesTest()
127 | {
128 | GVector3 point1 = new GVector3(0, 1, 1);
129 | GVector3 point2 = new GVector3(0, 0, 1);
130 | GVector3 point3 = new GVector3(1, 0, 1);
131 |
132 | GPlane target = PlaneS.CreateFromVertices(point1, point2, point3);
133 | GPlane expected = new GPlane(new GVector3(0, 0, 1), -1);
134 | Assert.Equal(target, expected);
135 | }
136 |
137 | // A test for Plane.CreateFromVertices
138 | [Fact]
139 | public void PlaneCreateFromVerticesTest2()
140 | {
141 | GVector3 point1 = new GVector3(0, 0, 1);
142 | GVector3 point2 = new GVector3(1, 0, 0);
143 | GVector3 point3 = new GVector3(1, 1, 0);
144 |
145 | GPlane target = PlaneS.CreateFromVertices(point1, point2, point3);
146 | GScalar invRoot2 = (GScalar)(1.0 / Math.Sqrt(2));
147 |
148 | GPlane expected = new GPlane(new GVector3(invRoot2, 0, invRoot2), -invRoot2);
149 | Assert.True(GenericMathHelper.Equal(target, expected), "GPlane.cstor did not return the expected value.");
150 | }
151 |
152 | // A test for Plane (Vector3, scalar)
153 | [Fact]
154 | public void PlaneConstructorTest3()
155 | {
156 | GVector3 normal = new GVector3(1, 2, 3);
157 | GScalar d = 4;
158 |
159 | GPlane target = new GPlane(normal, d);
160 | Assert.True(
161 | target.Normal == normal && target.Distance == d,
162 | "GPlane.cstor did not return the expected value.");
163 | }
164 |
165 | // A test for GPlane (Vector4)
166 | [Fact]
167 | public void PlaneConstructorTest()
168 | {
169 | GVector4 value = new GVector4(1, 2, 3, 4);
170 | GPlane target = new GPlane(value);
171 |
172 | Assert.True(
173 | target.Normal.X == value.X && target.Normal.Y == value.Y && target.Normal.Z == value.Z && target.Distance == value.W,
174 | "GPlane.cstor did not return the expected value.");
175 | }
176 |
177 | [Fact]
178 | public void PlaneDotTest()
179 | {
180 | GPlane target = new GPlane(2, 3, 4, 5);
181 | GVector4 value = new GVector4(5, 4, 3, 2);
182 |
183 | GScalar expected = 10 + 12 + 12 + 10;
184 | GScalar actual = PlaneS.Dot(target, value);
185 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Dot returns unexpected value.");
186 | }
187 |
188 | [Fact]
189 | public void PlaneDotCoordinateTest()
190 | {
191 | GPlane target = new GPlane(2, 3, 4, 5);
192 | GVector3 value = new GVector3(5, 4, 3);
193 |
194 | GScalar expected = 10 + 12 + 12 + 5;
195 | GScalar actual = PlaneS.DotCoordinate(target, value);
196 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.DotCoordinate returns unexpected value.");
197 | }
198 |
199 | [Fact]
200 | public void PlaneDotNormalTest()
201 | {
202 | GPlane target = new GPlane(2, 3, 4, 5);
203 | GVector3 value = new GVector3(5, 4, 3);
204 |
205 | GScalar expected = 10 + 12 + 12;
206 | GScalar actual = PlaneS.DotNormal(target, value);
207 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.DotCoordinate returns unexpected value.");
208 | }
209 |
210 | [Fact]
211 | public void PlaneNormalizeTest()
212 | {
213 | GPlane target = new GPlane(1, 2, 3, 4);
214 |
215 | GScalar f = target.Normal.LengthSquared();
216 | GScalar invF = (GScalar)(1.0 / Math.Sqrt(f));
217 | GPlane expected = new GPlane(target.Normal * invF, target.Distance * invF);
218 |
219 | GPlane actual = PlaneS.Normalize(target);
220 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Normalize returns unexpected value.");
221 |
222 | // normalize, normalized normal.
223 | actual = PlaneS.Normalize(actual);
224 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Normalize returns unexpected value.");
225 | }
226 |
227 | [Fact]
228 | // Transform by matrix
229 | public void PlaneTransformTest1()
230 | {
231 | GPlane target = new GPlane(1, 2, 3, 4);
232 | target = PlaneS.Normalize(target);
233 |
234 | GMatrix4x4 m =
235 | Matrix4x4S.CreateRotationX(GenericMathHelper.ToRadians((GScalar)30)) *
236 | Matrix4x4S.CreateRotationY(GenericMathHelper.ToRadians((GScalar)30)) *
237 | Matrix4x4S.CreateRotationZ(GenericMathHelper.ToRadians((GScalar)30));
238 | m = m with { M41 = 10, M42 = 20, M43 = 30 };
239 |
240 | GPlane expected = new GPlane();
241 | GMatrix4x4 inv;
242 | Matrix4x4S.Invert(m, out inv);
243 | GMatrix4x4 itm = Matrix4x4S.Transpose(inv);
244 | GScalar x = target.Normal.X, y = target.Normal.Y, z = target.Normal.Z, w = target.Distance;
245 | expected = expected with
246 | {
247 | Normal = new GVector3(
248 | x * itm.M11 + y * itm.M21 + z * itm.M31 + w * itm.M41,
249 | x * itm.M12 + y * itm.M22 + z * itm.M32 + w * itm.M42,
250 | x * itm.M13 + y * itm.M23 + z * itm.M33 + w * itm.M43)
251 | };
252 | expected = expected with { Distance = x * itm.M14 + y * itm.M24 + z * itm.M34 + w * itm.M44 };
253 |
254 | GPlane actual;
255 | actual = PlaneS.Transform(target, m);
256 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Transform did not return the expected value.");
257 | }
258 |
259 | [Fact]
260 | // Transform by quaternion
261 | public void PlaneTransformTest2()
262 | {
263 | GPlane target = new GPlane(1, 2, 3, 4);
264 | target = PlaneS.Normalize(target);
265 |
266 | GMatrix4x4 m =
267 | Matrix4x4S.CreateRotationX(GenericMathHelper.ToRadians((GScalar)30)) *
268 | Matrix4x4S.CreateRotationY(GenericMathHelper.ToRadians((GScalar)30)) *
269 | Matrix4x4S.CreateRotationZ(GenericMathHelper.ToRadians((GScalar)30));
270 | GQuaternion q = QuaternionS.CreateFromRotationMatrix(m);
271 |
272 | GPlane expected = new GPlane();
273 | GScalar x = target.Normal.X, y = target.Normal.Y, z = target.Normal.Z, w = target.Distance;
274 | expected = expected with
275 | {
276 | Normal = new GVector3(
277 | x * m.M11 + y * m.M21 + z * m.M31 + w * m.M41,
278 | x * m.M12 + y * m.M22 + z * m.M32 + w * m.M42,
279 | x * m.M13 + y * m.M23 + z * m.M33 + w * m.M43)
280 | };
281 | expected = expected with { Distance = x * m.M14 + y * m.M24 + z * m.M34 + w * m.M44 };
282 |
283 | GPlane actual;
284 | actual = PlaneS.Transform(target, q);
285 | Assert.True(GenericMathHelper.Equal(expected, actual), "GPlane.Transform did not return the expected value.");
286 | }
287 |
288 | // A test for Plane comparison involving NaN values
289 | [Fact]
290 | public void PlaneEqualsNanTest()
291 | {
292 | GPlane a = new GPlane(GScalar.NaN, 0, 0, 0);
293 | GPlane b = new GPlane(0, GScalar.NaN, 0, 0);
294 | GPlane c = new GPlane(0, 0, GScalar.NaN, 0);
295 | GPlane d = new GPlane(0, 0, 0, GScalar.NaN);
296 |
297 | Assert.False(a == new GPlane(0, 0, 0, 0));
298 | Assert.False(b == new GPlane(0, 0, 0, 0));
299 | Assert.False(c == new GPlane(0, 0, 0, 0));
300 | Assert.False(d == new GPlane(0, 0, 0, 0));
301 |
302 | Assert.True(a != new GPlane(0, 0, 0, 0));
303 | Assert.True(b != new GPlane(0, 0, 0, 0));
304 | Assert.True(c != new GPlane(0, 0, 0, 0));
305 | Assert.True(d != new GPlane(0, 0, 0, 0));
306 |
307 | Assert.False(a.Equals(new GPlane(0, 0, 0, 0)));
308 | Assert.False(b.Equals(new GPlane(0, 0, 0, 0)));
309 | Assert.False(c.Equals(new GPlane(0, 0, 0, 0)));
310 | Assert.False(d.Equals(new GPlane(0, 0, 0, 0)));
311 |
312 | // Counterintuitive result - IEEE rules for NaN comparison are weird!
313 | Assert.False(a.Equals(a));
314 | Assert.False(b.Equals(b));
315 | Assert.False(c.Equals(c));
316 | Assert.False(d.Equals(d));
317 | }
318 |
319 | // A test to make sure these types are blittable directly into GPU buffer memory layouts
320 | [Fact]
321 | public unsafe void PlaneSizeofTest()
322 | {
323 | int scalarSize = sizeof(GScalar);
324 | #pragma warning disable xUnit2000 // Constants and literals should be the expected argument
325 | Assert.Equal(scalarSize * 4, sizeof(GPlane));
326 | Assert.Equal(scalarSize * 8, sizeof(Plane_2x));
327 | Assert.Equal(scalarSize * 5, sizeof(PlanePlusSingle));
328 | Assert.Equal(scalarSize * 10, sizeof(PlanePlusFloatSingle_2x));
329 | #pragma warning restore xUnit2000 // Constants and literals should be the expected argument
330 | }
331 |
332 | [Fact]
333 | public void PlaneToStringTest()
334 | {
335 | GPlane target = new GPlane(1, 2, 3, 4);
336 | string expected = string.Format(
337 | CultureInfo.CurrentCulture,
338 | "{{Normal:{0:G} Distance:{1}}}",
339 | target.Normal,
340 | target.Distance);
341 |
342 | Assert.Equal(expected, target.ToString());
343 | }
344 |
345 | [StructLayout(LayoutKind.Sequential)]
346 | struct Plane_2x
347 | {
348 | private GPlane _a;
349 | private GPlane _b;
350 | }
351 |
352 | [StructLayout(LayoutKind.Sequential)]
353 | struct PlanePlusSingle
354 | {
355 | private GPlane _v;
356 | private GScalar _f;
357 | }
358 |
359 | [StructLayout(LayoutKind.Sequential)]
360 | struct PlanePlusFloatSingle_2x
361 | {
362 | private PlanePlusSingle _a;
363 | private PlanePlusSingle _b;
364 | }
365 | }
366 | }
367 |
--------------------------------------------------------------------------------
/GenericVectors/Tests/GenericVector2Tests.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | using Xunit;
5 |
6 | namespace System.Numerics.Tests
7 | {
8 | public class GenericVector2Tests
9 | {
10 | // A test for operator + (Vector2, Vector2)
11 | [Fact]
12 | public void Vector2AdditionTestHalf() => Vector2AdditionTest();
13 | [Fact]
14 | public void Vector2AdditionTestFloat() => Vector2AdditionTest();
15 | [Fact]
16 | public void Vector2AdditionTestDouble() => Vector2AdditionTest();
17 |
18 | private static void Vector2AdditionTest()
19 | where T : struct, IFloatingPointIeee754
20 | {
21 | Vector2 a = new Vector2(T.One, T.CreateChecked(2.0));
22 | Vector2 b = new Vector2(T.CreateChecked(3.0), T.CreateChecked(4.0));
23 |
24 | Vector2 expected = new Vector2(T.CreateChecked(4.0), T.CreateChecked(6.0));
25 | Vector2 actual;
26 |
27 | actual = a + b;
28 |
29 | Assert.True(GenericMathHelper.Equal(expected, actual), "Vector2.operator + did not return the expected value.");
30 | }
31 |
32 | [Fact]
33 | public void Vector2AdditiveIdentityHalfTest() => Vector2AdditiveIdentityTest();
34 | [Fact]
35 | public void Vector2AdditiveIdentitySingleTest() => Vector2AdditiveIdentityTest();
36 | [Fact]
37 | public void Vector2AdditiveIdentityDoubleTest() => Vector2AdditiveIdentityTest();
38 |
39 | static void Vector2AdditiveIdentityTest()
40 | where T : struct, IFloatingPointIeee754
41 | {
42 | Vector2 v1 = new Vector2(T.CreateChecked(42), T.CreateChecked(142));
43 | Vector2 actual = Add(v1);
44 | Vector2 expected = v1;
45 | Assert.Equal(actual, expected);
46 |
47 | static T Add(T value)
48 | where T : IAdditiveIdentity, IAdditionOperators
49 | {
50 | return T.AdditiveIdentity + value;
51 | }
52 | }
53 |
54 | [Fact]
55 | public void Vector2MultiplicativeIdentityHalfTest() => Vector2MultiplicativeIdentityTest();
56 | [Fact]
57 | public void Vector2MultiplicativeIdentitySingleTest() => Vector2MultiplicativeIdentityTest();
58 | [Fact]
59 | public void Vector2MultiplicativeIdentityDoubleTest() => Vector2MultiplicativeIdentityTest();
60 |
61 | static void Vector2MultiplicativeIdentityTest()
62 | where T : struct, IFloatingPointIeee754
63 | {
64 | Vector2 v1 = new Vector2(T.CreateChecked(42), T.CreateChecked(142));
65 | Vector2 actual = Multiply(v1);
66 | Vector2 expected = v1;
67 | Assert.Equal(actual, expected);
68 |
69 | static T Multiply(T value)
70 | where T : IMultiplicativeIdentity, IMultiplyOperators
71 | {
72 | return T.MultiplicativeIdentity * value;
73 | }
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/GenericVectors/src/Matrix3x2S.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | using System.Runtime.CompilerServices;
5 |
6 | namespace System.Numerics
7 | {
8 | public partial struct Matrix3x2S
9 | {
10 | // "Friendly" Operators
11 |
12 | /// Computes the unary plus of a value.
13 | /// The value for which to compute its unary plus.
14 | /// The unary plus of .
15 | public static Matrix3x2 Plus(in Matrix3x2 value)
16 | where T : struct, IFloatingPointIeee754
17 | {
18 | return value;
19 | }
20 |
21 | /// Negates the specified matrix by multiplying all its values by -1.
22 | /// The matrix to negate.
23 | /// The negated matrix.
24 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
25 | public static Matrix3x2 Negate(in Matrix3x2 value)
26 | where T : struct, IFloatingPointIeee754
27 | {
28 | return -value;
29 | }
30 |
31 | /// Adds each element in one matrix with its corresponding element in a second matrix.
32 | /// The first matrix.
33 | /// The second matrix.
34 | /// The matrix that contains the summed values of and .
35 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
36 | public static Matrix3x2 Add(in Matrix3x2 left, in Matrix3x2 right)
37 | where T : struct, IFloatingPointIeee754
38 | {
39 | return left + right;
40 | }
41 |
42 | /// Subtracts each element in a second matrix from its corresponding element in a first matrix.
43 | /// The first matrix.
44 | /// The second matrix.
45 | /// The matrix containing the values that result from subtracting each element in from its corresponding element in .
46 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
47 | public static Matrix3x2 Subtract(in Matrix3x2 left, in Matrix3x2 right)
48 | where T : struct, IFloatingPointIeee754
49 | {
50 | return left - right;
51 | }
52 |
53 | /// Multiplies two matrices together to compute the product.
54 | /// The first matrix.
55 | /// The second matrix.
56 | /// The product matrix.
57 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
58 | public static Matrix3x2 Multiply(in Matrix3x2 left, in Matrix3x2 right)
59 | where T : struct, IFloatingPointIeee754
60 | {
61 | return left * right;
62 | }
63 |
64 | ///
65 | /// Scales all elements in a matrix by the given scalar factor.
66 | ///
67 | /// The source matrix.
68 | /// The scaling value to use.
69 | /// The resulting matrix.
70 | public static Matrix3x2 Multiply(in Matrix3x2 left, T right)
71 | where T : struct, IFloatingPointIeee754
72 | {
73 | return left * right;
74 | }
75 |
76 | // Static Methods
77 |
78 | /// Creates a rotation matrix using the given rotation in radians.
79 | /// The amount of rotation, in radians.
80 | /// The rotation matrix.
81 | public static Matrix3x2 CreateRotation(T radians)
82 | where T : struct, IFloatingPointIeee754
83 | {
84 | T epsilon = T.CreateChecked(0.001 * Math.PI / 180.0); // 0.1% of a degree
85 |
86 | radians = T.Ieee754Remainder(radians, T.Tau);
87 |
88 | T c, s;
89 | if (radians > -epsilon && radians < epsilon)
90 | {
91 | // Exact case for zero rotation.
92 | c = T.One;
93 | s = T.Zero;
94 | }
95 | else if (radians > T.Pi / T.CreateChecked(2.0) - epsilon && radians < T.Pi / T.CreateChecked(2.0) + epsilon)
96 | {
97 | // Exact case for 90 degree rotation.
98 | c = T.Zero;
99 | s = T.One;
100 | }
101 | else if (radians < -T.Pi + epsilon || radians > T.Pi - epsilon)
102 | {
103 | // Exact case for 180 degree rotation.
104 | c = -T.One;
105 | s = T.Zero;
106 | }
107 | else if (radians > -T.Pi / T.CreateChecked(2.0) - epsilon && radians < -T.Pi / T.CreateChecked(2.0) + epsilon)
108 | {
109 | // Exact case for 270 degree rotation.
110 | c = T.Zero;
111 | s = -T.One;
112 | }
113 | else
114 | {
115 | // Arbitrary rotation.
116 | c = T.Cos(radians);
117 | s = T.Sin(radians);
118 | }
119 |
120 | // [ c s ]
121 | // [ -s c ]
122 | // [ 0 0 ]
123 | return new(
124 | c, s,
125 | -s, c,
126 | T.Zero, T.Zero);
127 | }
128 |
129 | /// Creates a rotation matrix using the specified rotation in radians and a center point.
130 | /// The amount of rotation, in radians.
131 | /// The center point.
132 | /// The rotation matrix.
133 | public static Matrix3x2 CreateRotation(T radians, in Vector2 centerPoint)
134 | where T : struct, IFloatingPointIeee754
135 | {
136 | T epsilon = T.CreateChecked(0.001 * Math.PI / 180.0); // 0.1% of a degree
137 |
138 | radians = T.Ieee754Remainder(radians, T.Tau);
139 |
140 | T c, s;
141 | if (radians > -epsilon && radians < epsilon)
142 | {
143 | // Exact case for zero rotation.
144 | c = T.One;
145 | s = T.Zero;
146 | }
147 | else if (radians > T.Pi / T.CreateChecked(2.0) - epsilon && radians < T.Pi / T.CreateChecked(2.0) + epsilon)
148 | {
149 | // Exact case for 90 degree rotation.
150 | c = T.Zero;
151 | s = T.One;
152 | }
153 | else if (radians < -T.Pi + epsilon || radians > T.Pi - epsilon)
154 | {
155 | // Exact case for 180 degree rotation.
156 | c = -T.One;
157 | s = T.Zero;
158 | }
159 | else if (radians > -T.Pi / T.CreateChecked(2.0) - epsilon && radians < -T.Pi / T.CreateChecked(2.0) + epsilon)
160 | {
161 | // Exact case for 270 degree rotation.
162 | c = T.Zero;
163 | s = -T.One;
164 | }
165 | else
166 | {
167 | // Arbitrary rotation.
168 | c = T.Cos(radians);
169 | s = T.Sin(radians);
170 | }
171 |
172 | T tx = centerPoint.X * (T.One - c) + centerPoint.Y * s;
173 | T ty = centerPoint.Y * (T.One - c) - centerPoint.X * s;
174 |
175 | // [ c s ]
176 | // [ -s c ]
177 | // [ tx ty ]
178 | return new(
179 | c, s,
180 | -s, c,
181 | tx, ty);
182 | }
183 |
184 | /// Creates a scaling matrix that scales uniformly with the given scale.
185 | /// The uniform scale to use.
186 | /// The scaling matrix.
187 | public static Matrix3x2 CreateScale(T scale)
188 | where T : struct, IFloatingPointIeee754
189 | {
190 | return new(
191 | scale, T.Zero,
192 | T.Zero, scale,
193 | T.Zero, T.Zero);
194 | }
195 |
196 | /// Creates a scaling matrix that scales uniformly with the specified scale with an offset from the specified center.
197 | /// The uniform scale to use.
198 | /// The center offset.
199 | /// The scaling matrix.
200 | public static Matrix3x2 CreateScale(T scale, in Vector2 centerPoint)
201 | where T : struct, IFloatingPointIeee754
202 | {
203 | T tx = centerPoint.X * (T.One - scale);
204 | T ty = centerPoint.Y * (T.One - scale);
205 |
206 | return new(
207 | scale, T.Zero,
208 | T.Zero, scale,
209 | tx, ty);
210 | }
211 |
212 | /// Creates a scaling matrix from the specified X and Y components.
213 | /// The value to scale by on the X axis.
214 | /// The value to scale by on the Y axis.
215 | /// The scaling matrix.
216 | public static Matrix3x2 CreateScale(T scaleX, T scaleY)
217 | where T : struct, IFloatingPointIeee754
218 | {
219 | return new(
220 | scaleX, T.Zero,
221 | T.Zero, scaleY,
222 | T.Zero, T.Zero);
223 | }
224 |
225 | /// Creates a scaling matrix that is offset by a given center point.
226 | /// The value to scale by on the X axis.
227 | /// The value to scale by on the Y axis.
228 | /// The center point.
229 | /// The scaling matrix.
230 | public static Matrix3x2 CreateScale(T scaleX, T scaleY, in Vector2 centerPoint)
231 | where T : struct, IFloatingPointIeee754
232 | {
233 | T tx = centerPoint.X * (T.One - scaleX);
234 | T ty = centerPoint.Y * (T.One - scaleY);
235 |
236 | return new(
237 | scaleX, T.Zero,
238 | T.Zero, scaleY,
239 | tx, ty);
240 | }
241 |
242 | /// Creates a scaling matrix from the specified vector scale.
243 | /// The scale to use.
244 | /// The scaling matrix.
245 | public static Matrix3x2 CreateScale(in Vector2 scale)
246 | where T : struct, IFloatingPointIeee754
247 | {
248 | return new(
249 | scale.X, T.Zero,
250 | T.Zero, scale.Y,
251 | T.Zero, T.Zero);
252 | }
253 |
254 | /// Creates a scaling matrix from the specified vector scale with an offset from the specified center point.
255 | /// The scale to use.
256 | /// The center offset.
257 | /// The scaling matrix.
258 | public static Matrix3x2 CreateScale(in Vector2 scale, in Vector2 centerPoint)
259 | where T : struct, IFloatingPointIeee754
260 | {
261 | T tx = centerPoint.X * (T.One - scale.X);
262 | T ty = centerPoint.Y * (T.One - scale.Y);
263 |
264 | return new(
265 | scale.X, T.Zero,
266 | T.Zero, scale.Y,
267 | tx, ty);
268 | }
269 |
270 | /// Creates a skew matrix from the specified angles in radians.
271 | /// The X angle, in radians.
272 | /// The Y angle, in radians.
273 | /// The skew matrix.
274 | public static Matrix3x2 CreateSkew(T radiansX, T radiansY)
275 | where T : struct, IFloatingPointIeee754
276 | {
277 | T xTan = T.Tan(radiansX);
278 | T yTan = T.Tan(radiansY);
279 |
280 | return new(
281 | T.One, yTan,
282 | xTan, T.One,
283 | T.Zero, T.Zero);
284 | }
285 |
286 | /// Creates a skew matrix from the specified angles in radians and a center point.
287 | /// The X angle, in radians.
288 | /// The Y angle, in radians.
289 | /// The center point.
290 | /// The skew matrix.
291 | public static Matrix3x2 CreateSkew(T radiansX, T radiansY, in Vector2 centerPoint)
292 | where T : struct, IFloatingPointIeee754
293 | {
294 | T xTan = T.Tan(radiansX);
295 | T yTan = T.Tan(radiansY);
296 |
297 | T tx = -centerPoint.Y * xTan;
298 | T ty = -centerPoint.X * yTan;
299 |
300 | return new(
301 | T.One, yTan,
302 | xTan, T.One,
303 | tx, ty);
304 | }
305 |
306 | /// Creates a translation matrix from the specified X and Y components.
307 | /// The X position.
308 | /// The Y position.
309 | /// The translation matrix.
310 | public static Matrix3x2 CreateTranslation(T positionX, T positionY)
311 | where T : struct, IFloatingPointIeee754
312 | {
313 | return new(
314 | T.One, T.Zero,
315 | T.Zero, T.One,
316 | positionX, positionY);
317 | }
318 |
319 | /// Creates a translation matrix from the specified 2-dimensional vector.
320 | /// The translation position.
321 | /// The translation matrix.
322 | public static Matrix3x2 CreateTranslation(in Vector2 position)
323 | where T : struct, IFloatingPointIeee754
324 | {
325 | return new(
326 | T.One, T.Zero,
327 | T.Zero, T.One,
328 | position.X, position.Y);
329 | }
330 |
331 | /// Tries to invert the specified matrix. The return value indicates whether the operation succeeded.
332 | /// The matrix to invert.
333 | /// When this method returns, contains the inverted matrix if the operation succeeded.
334 | /// if was converted successfully; otherwise, .
335 | public static bool Invert(in Matrix3x2 matrix, out Matrix3x2 result)
336 | where T : struct, IFloatingPointIeee754
337 | {
338 | T det = (matrix.M11 * matrix.M22) - (matrix.M21 * matrix.M12);
339 |
340 | if (T.Abs(det) < T.Epsilon)
341 | {
342 | result = new(T.NaN, T.NaN, T.NaN, T.NaN, T.NaN, T.NaN);
343 | return false;
344 | }
345 |
346 | T invDet = T.One / det;
347 |
348 | result = new(
349 | matrix.M22 * invDet,
350 | -matrix.M12 * invDet,
351 | -matrix.M21 * invDet,
352 | matrix.M11 * invDet,
353 | (matrix.M21 * matrix.M32 - matrix.M31 * matrix.M22) * invDet,
354 | (matrix.M31 * matrix.M12 - matrix.M11 * matrix.M32) * invDet);
355 |
356 | return true;
357 | }
358 |
359 | /// Performs a linear interpolation from one matrix to a second matrix based on a value that specifies the weighting of the second matrix.
360 | /// The first matrix.
361 | /// The second matrix.
362 | /// The relative weighting of .
363 | /// The interpolated matrix.
364 | public static Matrix3x2 Lerp(in Matrix3x2 min, in Matrix3x2 max, T amount)
365 | where T : struct, IFloatingPointIeee754
366 | {
367 | return new(
368 | // First row
369 | min.M11 + (max.M11 - min.M11) * amount,
370 | min.M12 + (max.M12 - min.M12) * amount,
371 |
372 | // Second row
373 | min.M21 + (max.M21 - min.M21) * amount,
374 | min.M22 + (max.M22 - min.M22) * amount,
375 |
376 | // Third row
377 | min.M31 + (max.M31 - min.M31) * amount,
378 | min.M32 + (max.M32 - min.M32) * amount);
379 | }
380 | }
381 | }
382 |
--------------------------------------------------------------------------------
/GenericVectors/src/Matrix3x2_1.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | using System.Globalization;
5 | using System.Runtime.CompilerServices;
6 | using System.Text;
7 |
8 | namespace System.Numerics
9 | {
10 | /// Represents a 3x2 matrix.
11 | ///
14 | [Intrinsic]
15 | public readonly record struct Matrix3x2 :
16 | IAdditionOperators, Matrix3x2, Matrix3x2>,
17 | IAdditiveIdentity, Matrix3x2>,
18 | IEquatable>,
19 | IEqualityOperators, Matrix3x2, bool>,
20 | IFormattable,
21 | IMultiplicativeIdentity, Matrix3x2>,
22 | IMultiplyOperators, Matrix3x2, Matrix3x2>,
23 | IMultiplyOperators, T, Matrix3x2>,
24 | ISubtractionOperators, Matrix3x2, Matrix3x2>,
25 | IUnaryNegationOperators, Matrix3x2>,
26 | IUnaryPlusOperators, Matrix3x2>
27 | where T : struct, IFloatingPointIeee754
28 | {
29 | // Fields
30 |
31 | /// The first element of the first row.
32 | public T M11 { get; init; }
33 |
34 | /// The second element of the first row.
35 | public T M12 { get; init; }
36 |
37 | /// The first element of the second row.
38 | public T M21 { get; init; }
39 |
40 | /// The second element of the second row.
41 | public T M22 { get; init; }
42 |
43 | /// The first element of the third row.
44 | public T M31 { get; init; }
45 |
46 | /// The second element of the third row.
47 | public T M32 { get; init; }
48 |
49 | // Constructors
50 |
51 | /// Creates a 3x2 matrix from the specified components.
52 | /// The value to assign to the first element in the first row.
53 | /// The value to assign to the second element in the first row.
54 | /// The value to assign to the first element in the second row.
55 | /// The value to assign to the second element in the second row.
56 | /// The value to assign to the first element in the third row.
57 | /// The value to assign to the second element in the third row.
58 | public Matrix3x2(T m11, T m12, T m21, T m22, T m31, T m32)
59 | {
60 | M11 = m11;
61 | M12 = m12;
62 | M21 = m21;
63 | M22 = m22;
64 | M31 = m31;
65 | M32 = m32;
66 | }
67 |
68 | // Static Properties
69 |
70 | /// Gets the multiplicative identity matrix.
71 | /// The multiplicative identify matrix.
72 | public static Matrix3x2 Identity { get; } = new(
73 | T.One, T.Zero,
74 | T.Zero, T.One,
75 | T.Zero, T.Zero);
76 |
77 | static Matrix3x2 IAdditiveIdentity, Matrix3x2>.AdditiveIdentity
78 | => new(T.Zero, T.Zero, T.Zero, T.Zero, T.Zero, T.Zero);
79 |
80 | static Matrix3x2 IMultiplicativeIdentity, Matrix3x2>.MultiplicativeIdentity => Identity;
81 |
82 | // Properties
83 |
84 |
85 | /// Gets a value that indicates whether the current matrix is the identity matrix.
86 | /// if the current matrix is the identity matrix; otherwise, .
87 | public bool IsIdentity
88 | {
89 | get
90 | {
91 | return M11 == T.One && M22 == T.One && // Check diagonal element first for early out.
92 | M12 == T.Zero && M21 == T.Zero &&
93 | M31 == T.Zero && M32 == T.Zero;
94 | }
95 | }
96 |
97 | /// Gets or sets the translation component of this matrix.
98 | /// The translation component of the current instance.
99 | public Vector2 Translation => new(M31, M32);
100 |
101 | // Operators
102 |
103 | /// Computes the unary plus of a value.
104 | /// The value for which to compute its unary plus.
105 | /// The unary plus of .
106 | public static Matrix3x2 operator +(Matrix3x2 value)
107 | {
108 | return new(
109 | value.M11,
110 | value.M12,
111 | value.M21,
112 | value.M22,
113 | value.M31,
114 | value.M32);
115 | }
116 |
117 | /// Negates the specified matrix by multiplying all its values by -1.
118 | /// The matrix to negate.
119 | /// The negated matrix.
120 | ///
121 | public static Matrix3x2 operator -(Matrix3x2 value)
122 | {
123 | return new(
124 | -value.M11,
125 | -value.M12,
126 | -value.M21,
127 | -value.M22,
128 | -value.M31,
129 | -value.M32);
130 | }
131 |
132 | /// Adds each element in one matrix with its corresponding element in a second matrix.
133 | /// The first matrix.
134 | /// The second matrix.
135 | /// The matrix that contains the summed values.
136 | /// The method defines the operation of the addition operator for objects.
137 | public static Matrix3x2 operator +(Matrix3x2 left, Matrix3x2 right)
138 | {
139 | return new(
140 | left.M11 + right.M11,
141 | left.M12 + right.M12,
142 | left.M21 + right.M21,
143 | left.M22 + right.M22,
144 | left.M31 + right.M31,
145 | left.M32 + right.M32);
146 | }
147 |
148 | /// Subtracts each element in a second matrix from its corresponding element in a first matrix.
149 | /// The first matrix.
150 | /// The second matrix.
151 | /// The matrix containing the values that result from subtracting each element in from its corresponding element in .
152 | /// The method defines the operation of the subtraction operator for objects.
153 | public static Matrix3x2 operator -(Matrix3x2 left, Matrix3x2 right)
154 | {
155 | return new(
156 | left.M11 - right.M11,
157 | left.M12 - right.M12,
158 | left.M21 - right.M21,
159 | left.M22 - right.M22,
160 | left.M31 - right.M31,
161 | left.M32 - right.M32);
162 | }
163 |
164 | /// Multiplies two matrices together to compute the product.
165 | /// The first matrix.
166 | /// The second matrix.
167 | /// The product matrix.
168 | /// The method defines the operation of the multiplication operator for objects.
169 | public static Matrix3x2 operator *(Matrix3x2 left, Matrix3x2 right)
170 | {
171 | return new(
172 | // First row
173 | left.M11 * right.M11 + left.M12 * right.M21,
174 | left.M11 * right.M12 + left.M12 * right.M22,
175 |
176 | // Second row
177 | left.M21 * right.M11 + left.M22 * right.M21,
178 | left.M21 * right.M12 + left.M22 * right.M22,
179 |
180 | // Third row
181 | left.M31 * right.M11 + left.M32 * right.M21 + right.M31,
182 | left.M31 * right.M12 + left.M32 * right.M22 + right.M32);
183 | }
184 |
185 | /// Multiplies a matrix by a float to compute the product.
186 | /// The matrix to scale.
187 | /// The scaling value to use.
188 | /// The scaled matrix.
189 | /// The method defines the operation of the multiplication operator for objects.
190 | public static Matrix3x2 operator *(Matrix3x2 left, T right)
191 | {
192 | return new(
193 | left.M11 * right,
194 | left.M12 * right,
195 | left.M21 * right,
196 | left.M22 * right,
197 | left.M31 * right,
198 | left.M32 * right);
199 | }
200 |
201 | // Methods
202 |
203 | /// Returns a value that indicates whether this instance and another 3x2 matrix are equal.
204 | /// The other matrix.
205 | /// if the two matrices are equal; otherwise, .
206 | /// Two matrices are equal if all their corresponding elements are equal.
207 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
208 | public readonly bool Equals(Matrix3x2 other)
209 | {
210 | // Check diagonal element first for early out.
211 | return (M11 == other.M11
212 | && M22 == other.M22
213 | && M12 == other.M12
214 | && M21 == other.M21
215 | && M31 == other.M31
216 | && M32 == other.M32);
217 | }
218 |
219 | /// Calculates the determinant for this matrix.
220 | /// The determinant.
221 | /// The determinant is calculated by expanding the matrix with a third column whose values are (0,0,1).
222 | public T GetDeterminant()
223 | {
224 | // There isn't actually any such thing as a determinant for a non-square matrix,
225 | // but this 3x2 type is really just an optimization of a 3x3 where we happen to
226 | // know the rightmost column is always (0, 0, 1). So we expand to 3x3 format:
227 | //
228 | // [ M11, M12, 0 ]
229 | // [ M21, M22, 0 ]
230 | // [ M31, M32, 1 ]
231 | //
232 | // Sum the diagonal products:
233 | // (M11 * M22 * 1) + (M12 * 0 * M31) + (0 * M21 * M32)
234 | //
235 | // Subtract the opposite diagonal products:
236 | // (M31 * M22 * 0) + (M32 * 0 * M11) + (1 * M21 * M12)
237 | //
238 | // Collapse out the constants and oh look, this is just a 2x2 determinant!
239 |
240 | return (M11 * M22) - (M21 * M12);
241 | }
242 |
243 | /// Returns a string that represents this matrix.
244 | /// The string representation of this matrix.
245 | /// The numeric values in the returned string are formatted by using the conventions of the current culture. For example, for the en-US culture, the returned string might appear as { {M11:1.1 M12:1.2} {M21:2.1 M22:2.2} {M31:3.1 M32:3.2} }.
246 | public override string ToString()
247 | => $"{{ {{M11:{M11} M12:{M12}}} {{M21:{M21} M22:{M22}}} {{M31:{M31} M32:{M32}}} }}";
248 |
249 | /// Returns the string representation of the current instance using the specified format string to format individual elements.
250 | /// A standard or custom numeric format string that defines the format of individual elements.
251 | /// The string representation of the current instance.
252 | /// This method returns a string in which each element of the vector is formatted using and the current culture's formatting conventions. The "<" and ">" characters are used to begin and end the string, and the current culture's property followed by a space is used to separate each element.
253 | /// Standard Numeric Format Strings
254 | /// Custom Numeric Format Strings
255 | public readonly string ToString(string? format)
256 | {
257 | return ToString(format, CultureInfo.CurrentCulture);
258 | }
259 | /// Returns the string representation of the current instance using the specified format string to format individual elements and the specified format provider to define culture-specific formatting.
260 | /// A standard or custom numeric format string that defines the format of individual elements.
261 | /// A format provider that supplies culture-specific formatting information.
262 | /// The string representation of the current instance.
263 | /// This method returns a string in which each element of the vector is formatted using and . The "<" and ">" characters are used to begin and end the string, and the format provider's property followed by a space is used to separate each element.
264 | /// Custom Numeric Format Strings
265 | /// Standard Numeric Format Strings
266 | public readonly string ToString(string? format, IFormatProvider? formatProvider)
267 | {
268 | StringBuilder sb = new StringBuilder();
269 | sb.Append("{ ");
270 |
271 | sb.Append('{');
272 | sb.Append(M11.ToString(format, formatProvider));
273 | sb.Append(' ');
274 | sb.Append(M12.ToString(format, formatProvider));
275 | sb.Append('}');
276 |
277 | sb.Append(' ');
278 |
279 | sb.Append('{');
280 | sb.Append(M21.ToString(format, formatProvider));
281 | sb.Append(' ');
282 | sb.Append(M22.ToString(format, formatProvider));
283 | sb.Append('}');
284 |
285 | sb.Append(' ');
286 |
287 | sb.Append('{');
288 | sb.Append(M31.ToString(format, formatProvider));
289 | sb.Append(' ');
290 | sb.Append(M32.ToString(format, formatProvider));
291 | sb.Append('}');
292 |
293 | sb.Append(" }");
294 | return sb.ToString();
295 | }
296 | }
297 | }
298 |
--------------------------------------------------------------------------------
/GenericVectors/src/PlaneS.cs:
--------------------------------------------------------------------------------
1 | // Licensed to the .NET Foundation under one or more agreements.
2 | // The .NET Foundation licenses this file to you under the MIT license.
3 |
4 | using System.Runtime.CompilerServices;
5 |
6 | namespace System.Numerics
7 | {
8 | public partial struct PlaneS
9 | {
10 | // Static Methods
11 |
12 | /// Creates a object that contains three specified points.
13 | /// The first point defining the plane.
14 | /// The second point defining the plane.
15 | /// The third point defining the plane.
16 | /// The plane containing the three points.
17 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
18 | public static Plane CreateFromVertices(in Vector3 point1, in Vector3 point2, in Vector3 point3)
19 | where T : struct, IFloatingPointIeee754
20 | {
21 | T ax = point2.X - point1.X;
22 | T ay = point2.Y - point1.Y;
23 | T az = point2.Z - point1.Z;
24 |
25 | T bx = point3.X - point1.X;
26 | T by = point3.Y - point1.Y;
27 | T bz = point3.Z - point1.Z;
28 |
29 | // N=Cross(a,b)
30 | T nx = ay * bz - az * by;
31 | T ny = az * bx - ax * bz;
32 | T nz = ax * by - ay * bx;
33 |
34 | // Normalize(N)
35 | T ls = nx * nx + ny * ny + nz * nz;
36 | T invNorm = T.One / T.Sqrt(ls);
37 |
38 | Vector3 normal = new(
39 | nx * invNorm,
40 | ny * invNorm,
41 | nz * invNorm);
42 |
43 | return new(
44 | normal,
45 | -(normal.X * point1.X + normal.Y * point1.Y + normal.Z * point1.Z));
46 | }
47 |
48 | /// Calculates the dot product of a plane and a 4-dimensional vector.
49 | /// The plane.
50 | /// The four-dimensional vector.
51 | /// The dot product.
52 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
53 | public static T Dot(in Plane left, in Vector4 right)
54 | where T : struct, IFloatingPointIeee754
55 | {
56 | return left.Normal.X * right.X +
57 | left.Normal.Y * right.Y +
58 | left.Normal.Z * right.Z +
59 | left.Distance * right.W;
60 | }
61 |
62 | /// Returns the dot product of a specified three-dimensional vector and the normal vector of this plane plus the distance () value of the plane.
63 | /// The plane.
64 | /// The 3-dimensional vector.
65 | /// The dot product.
66 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
67 | public static T DotCoordinate(in Plane left, in Vector3 right)
68 | where T : struct, IFloatingPointIeee754
69 | {
70 | return left.Normal.X * right.X +
71 | left.Normal.Y * right.Y +
72 | left.Normal.Z * right.Z +
73 | left.Distance;
74 | }
75 |
76 | /// Returns the dot product of a specified three-dimensional vector and the vector of this plane.
77 | /// The plane.
78 | /// The three-dimensional vector.
79 | /// The dot product.
80 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
81 | public static T DotNormal(in Plane left, in Vector3 right)
82 | where T : struct, IFloatingPointIeee754
83 | {
84 | return left.Normal.X * right.X +
85 | left.Normal.Y * right.Y +
86 | left.Normal.Z * right.Z;
87 | }
88 |
89 | ///