Skip to content

Commit

Permalink
brought over new anyalysis options and formatted the code to follow s…
Browse files Browse the repository at this point in the history
…uite. Replaced vector_math with vector_math_64. google/vector_math.dart#210
  • Loading branch information
Mav committed Mar 15, 2024
1 parent e7d5061 commit 55e3725
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 50 deletions.
21 changes: 18 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ linter:
prefer_final_in_for_each: true
use_build_context_synchronously: true
always_declare_return_types: true
always_specify_types: false
omit_local_variable_types: true
avoid_types_on_closure_parameters: true
always_specify_types: true
void_checks: true
use_raw_strings: true
unnecessary_this: true
unnecessary_string_interpolations: true
unnecessary_string_escapes: true
unnecessary_null_aware_assignments: true
unnecessary_new: true
unnecessary_late: true
unnecessary_lambdas: true
unnecessary_brace_in_string_interps: true
prefer_asserts_in_initializer_lists: true
prefer_asserts_with_message: true
lines_longer_than_80_chars: true
avoid_setters_without_getters: true
avoid_return_types_on_setters: true
annotate_overrides: true
always_put_required_named_parameters_first: true
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'dart:math' as math;
import 'package:vector_math/vector_math.dart' show Vector3;
import 'package:vector_math/vector_math_64.dart' show Vector3;
import 'package:object_3d/object_3d.dart';

void main() {
Expand Down
94 changes: 48 additions & 46 deletions lib/object_3d.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vector_math/vector_math.dart' as vmath;
import 'package:vector_math/vector_math.dart' show Vector3;
import 'package:vector_math/vector_math_64.dart' show Vector3, Matrix4;

typedef FaceColorFunc = Face Function(Face face);

Expand Down Expand Up @@ -58,8 +57,8 @@ class Face {
if (_cachedNormal != null) return Vector3.copy(_cachedNormal!);

// Normal needs recalculating
final p = Vector3.copy(_v2)..sub(_v1);
final q = Vector3.copy(_v2)..sub(_v3);
final Vector3 p = Vector3.copy(_v2)..sub(_v1);
final Vector3 q = Vector3.copy(_v2)..sub(_v3);
_cachedNormal = p.cross(q).normalized();

return Vector3.copy(_cachedNormal!);
Expand All @@ -68,8 +67,8 @@ class Face {

class Object3D extends StatefulWidget {
const Object3D({
super.key,
required this.size,
super.key,
this.color = Colors.white,
this.object,
this.path,
Expand Down Expand Up @@ -119,7 +118,7 @@ class _Object3DState extends State<Object3D> {
double _pitch = 15.0, _yaw = 45.0;
double? _previousX, _previousY;
double _deltaX = 0.0, _deltaY = 0.0;
List<Vector3> vertices = <vmath.Vector3>[];
List<Vector3> vertices = <Vector3>[];
List<List<int>> faces = <List<int>>[];
late Timer _updateTimer;

Expand All @@ -136,10 +135,10 @@ class _Object3DState extends State<Object3D> {
_updateTimer = Timer.periodic(const Duration(milliseconds: 16), (_) {
if (!mounted) return;
setState(() {
final adx = _deltaX.abs();
final ady = _deltaY.abs();
final sx = _deltaX < 0 ? -1 : 1;
final sy = _deltaY < 0 ? -1 : 1;
final double adx = _deltaX.abs();
final double ady = _deltaY.abs();
final int sx = _deltaX < 0 ? -1 : 1;
final int sy = _deltaY < 0 ? -1 : 1;

_deltaX = math.min(widget.maxSpeed, adx) * sx * widget.dampCoef;
_deltaY = math.min(widget.maxSpeed, ady) * sy * widget.dampCoef;
Expand All @@ -160,16 +159,18 @@ class _Object3DState extends State<Object3D> {

/// Parse the object file.
void _parseObj(String obj) {
final vertices = <vmath.Vector3>[];
final faces = <List<int>>[];
final lines = obj.split('\n');
for (var line in lines) {
const space = ' ';
final List<Vector3> vertices = <Vector3>[];
final List<List<int>> faces = <List<int>>[];
final List<String> lines = obj.split('\n');
for (String line in lines) {
const String space = ' ';
line = line.replaceAll(RegExp(r'\s+'), space);

// Split into tokens and drop empty tokens
final chars =
line.split(space).where((v) => v.isNotEmpty).toList(growable: false);
final List<String> chars = line
.split(space)
.where((String v) => v.isNotEmpty)
.toList(growable: false);

if (chars.isEmpty) continue;

Expand All @@ -182,8 +183,8 @@ class _Object3DState extends State<Object3D> {
),
);
} else if (chars[0] == 'f') {
final face = <int>[];
for (var i = 1; i < chars.length; i++) {
final List<int> face = <int>[];
for (int i = 1; i < chars.length; i++) {
face.add(int.parse(chars[i].split('/')[0]));
}
faces.add(face);
Expand Down Expand Up @@ -248,8 +249,8 @@ class _ObjectPainter extends CustomPainter {
final List<Vector3> vertices;
final List<List<int>> faces;

final vmath.Vector3 camera = Vector3(0.0, 0.0, 0.0);
final vmath.Vector3 light = Vector3(0.0, 0.0, 100.0).normalized();
final Vector3 camera = Vector3(0.0, 0.0, 0.0);
final Vector3 light = Vector3(0.0, 0.0, 100.0).normalized();

final FaceColorFunc? faceColorFunc;

Expand All @@ -268,7 +269,7 @@ class _ObjectPainter extends CustomPainter {
/// Calculate the position of a vertex in the 3D space based
/// on the angle of rotation, view-port position and zoom.
Vector3 _calcVertex(Vector3 vertex) {
final t = vmath.Matrix4.translationValues(_viewPortX, _viewPortY, 0);
final Matrix4 t = Matrix4.translationValues(_viewPortX, _viewPortY, 0);
t.scale(zoom, -zoom);
t.rotateX(_degreeToRadian(pitch));
t.rotateY(_degreeToRadian(yaw));
Expand All @@ -283,15 +284,15 @@ class _ObjectPainter extends CustomPainter {

/// Calculate the 2D-positions of a vertex in the 3D space.
List<Offset> _drawFace(List<Vector3> vertices, List<int> face) {
final coordinates = <Offset>[];
for (var i = 0; i < face.length; i++) {
final List<Offset> coordinates = <Offset>[];
for (int i = 0; i < face.length; i++) {
double x, y;
if (i < face.length - 1) {
final iV = vertices[face[i + 1] - 1];
final Vector3 iV = vertices[face[i + 1] - 1];
x = iV.x.toDouble();
y = iV.y.toDouble();
} else {
final iV = vertices[face[0] - 1];
final Vector3 iV = vertices[face[0] - 1];
x = iV.x.toDouble();
y = iV.y.toDouble();
}
Expand All @@ -303,9 +304,9 @@ class _ObjectPainter extends CustomPainter {
/// Calculate the color of a vertex based on the
/// position of the vertex and the light.
Face _defaultFaceColor(Face face) {
final s = face.normal.dot(light);
final coefficient = math.max(0, s);
final c = Color.fromRGBO(
final double s = face.normal.dot(light);
final num coefficient = math.max(0, s);
final Color c = Color.fromRGBO(
(color.red * coefficient).round(),
(color.green * coefficient).round(),
(color.blue * coefficient).round(),
Expand All @@ -317,39 +318,40 @@ class _ObjectPainter extends CustomPainter {

/// Order vertices by the distance to the camera.
List<AvgZ> _sortVertices(List<Vector3> vertices) {
final avgOfZ = <AvgZ>[];
for (var i = 0; i < faces.length; i++) {
final face = faces[i];
var z = 0.0;
for (final i in face) {
final List<AvgZ> avgOfZ = <AvgZ>[];
for (int i = 0; i < faces.length; i++) {
final List<int> face = faces[i];
double z = 0.0;
for (final int i in face) {
z += vertices[i - 1].z;
}
avgOfZ.add(AvgZ(i, z));
}
avgOfZ.sort((a, b) => a.z.compareTo(b.z));
avgOfZ.sort((AvgZ a, AvgZ b) => a.z.compareTo(b.z));
return avgOfZ;
}

@override
void paint(Canvas canvas, Size size) {
// Calculate the position of the vertices in the 3D space.
final verticesToDraw = <vmath.Vector3>[];
for (final vertex in vertices) {
final defV = _calcVertex(Vector3.copy(vertex));
final List<Vector3> verticesToDraw = <Vector3>[];
for (final Vector3 vertex in vertices) {
final Vector3 defV = _calcVertex(Vector3.copy(vertex));
verticesToDraw.add(defV);
}
// Order vertices by the distance to the camera.
final avgOfZ = _sortVertices(verticesToDraw);
final List<AvgZ> avgOfZ = _sortVertices(verticesToDraw);

// Calculate the position of the vertices in the 2D space
// and calculate the colors of the vertices.
final List<Offset> offsets = [];
final List<Color> colors = [];
final List<Offset> offsets = <Offset>[];
final List<Color> colors = <Color>[];
for (int i = 0; i < faces.length; i++) {
final faceIdx = faces[avgOfZ[i].index];
final List<int> faceIdx = faces[avgOfZ[i].index];

// Allocate list with a fixed size of 3
final verts = List.filled(3, Vector3.zero(), growable: false);
final List<Vector3> verts =
List<Vector3>.filled(3, Vector3.zero(), growable: false);

verts[0] = verticesToDraw[faceIdx[0] - 1];
verts[1] = verticesToDraw[faceIdx[1] - 1];
Expand All @@ -364,15 +366,15 @@ class _ObjectPainter extends CustomPainter {
// Fallback on default color func if a custom one is not provided
face = faceColorFunc?.call(face) ?? _defaultFaceColor(face);

colors.addAll([face.c1, face.c2, face.c3]);
colors.addAll(<Color>[face.c1, face.c2, face.c3]);
offsets.addAll(_drawFace(verticesToDraw, faceIdx));
}

// Draw the vertices.
final paint = Paint();
final Paint paint = Paint();
paint.style = PaintingStyle.fill;
paint.color = color;
final v = Vertices(VertexMode.triangles, offsets, colors: colors);
final Vertices v = Vertices(VertexMode.triangles, offsets, colors: colors);
canvas.drawVertices(v, BlendMode.clear, paint);
}

Expand Down

0 comments on commit 55e3725

Please sign in to comment.