Skip to content

Commit

Permalink
Add 3d spherical display from snapped photos
Browse files Browse the repository at this point in the history
  • Loading branch information
tcgj committed Jan 9, 2021
1 parent 488cc3d commit 066f694
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 45 deletions.
11 changes: 7 additions & 4 deletions lib/helpers/sphere.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';
import 'dart:typed_data';
import 'dart:math' as math;
import 'dart:ui' as ui;
Expand Down Expand Up @@ -89,7 +90,9 @@ class _SphereState extends State<Sphere> with TickerProviderStateMixin {
final x0 = (lon + math.pi) * surfaceXRate;
final y0 = (math.pi / 2 - lat) * surfaceYRate;

final color = surface[(y0.toInt() * surfaceWidth + x0).toInt()];
final a = (x0 < 3*surfaceWidth/16 ? 0 : (x0 > 13*surfaceWidth/16 ? surfaceWidth : (16*x0-3*surfaceWidth)/10));

final color = surface[(y0.toInt() * surfaceWidth + a).toInt()];
sphere[(sphereY + x - minX).toInt()] = color;
}
}
Expand Down Expand Up @@ -151,8 +154,8 @@ class _SphereState extends State<Sphere> with TickerProviderStateMixin {
},
onScaleUpdate: (ScaleUpdateDetails details) {
final offset = details.focalPoint - _lastFocalPoint;
rotationX = _lastRotationX + offset.dy / radius;
rotationZ = _lastRotationZ - offset.dx / radius;
rotationX = max(-math.pi / 4, min(_lastRotationX + offset.dy / radius, math.pi / 4));
rotationZ = max(-math.pi / 4, min(_lastRotationZ - offset.dx / radius, math.pi / 4));
setState(() {});
},
onScaleEnd: (ScaleEndDetails details) {
Expand All @@ -161,7 +164,7 @@ class _SphereState extends State<Sphere> with TickerProviderStateMixin {
final t = (v / a).abs() * 1000;
final s = (v.sign * 0.5 * v * v / a) / radius;
rotationZController.duration = Duration(milliseconds: t.toInt());
rotationZAnimation = Tween<double>(begin: rotationZ, end: rotationZ + s).animate(CurveTween(curve: Curves.decelerate).animate(rotationZController));
rotationZAnimation = Tween<double>(begin: rotationZ, end: max(-math.pi / 4, min(rotationZ + s, math.pi / 4))).animate(CurveTween(curve: Curves.decelerate).animate(rotationZController));
rotationZController
..value = 0
..forward();
Expand Down
40 changes: 21 additions & 19 deletions lib/homepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class _HomePageState extends State<HomePage> {
bool isLoading = false;
String result = "";

String _path;
var sharedPreferences;

@override
void initState() {
super.initState();
restore();
_localPath();
}

restore() async {
Expand Down Expand Up @@ -185,29 +187,21 @@ class _HomePageState extends State<HomePage> {
return res;
}

Future<String> get _localPath async {
Future _localPath() async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
_path = directory.path;
}

Future<File> writeImage(String name, int index) async {
final path = await _localPath;
Directory('$path/$name').createSync();
String newImagePath = '$path/$name/$index.jpg';
if (_path == null) {
await _localPath();
}
Directory('$_path/$name').createSync();
String newImagePath = '$_path/$name/$index.jpg';

return _image.copySync(newImagePath);
}

Future<String> getImageDir(String name) async {
final path = await _localPath;
final imagePath = '$path/$name';
if (Directory(imagePath).existsSync()) {
return imagePath;
} else {
return null;
}
}

Future<void> addProf(String name) async {
int counter = sharedPreferences.getInt(name) ?? 0;
String escName = name.replaceAll(RegExp(' +'), '_');
Expand Down Expand Up @@ -358,10 +352,10 @@ class _HomePageState extends State<HomePage> {
isLoading = true;
});

_image = await compressImage(_image);
var compressed = await compressImage(_image);

print("Trying upload");
String cloudinaryUrl = await uploadImage(_image);
String cloudinaryUrl = await uploadImage(compressed);

// https://res.cloudinary.com/jcjc/image/upload/v1610096814/rwvskc2b1pd6z23ssc9d.jpg - has face
// https://res.cloudinary.com/jcjc/image/upload/v1610096965/zhmke8z8g7xe9nqapwae.jpg - no face
Expand Down Expand Up @@ -410,7 +404,11 @@ class _HomePageState extends State<HomePage> {
faceId = faceSimilarity.data[0]['persistedFaceId'];
String name = faceIdToName(faceId);
String id = faceIdToIndex(faceId);
showAlertDialog(context, name, id);
if (name == null || id == null) {
showFailDialog(context, "This person does not exist in our database.");
} else {
showAlertDialog(context, name, id);
}

setState(() {
isLoading = false;
Expand Down Expand Up @@ -567,7 +565,11 @@ class _HomePageState extends State<HomePage> {
onPressed: () {
Player.updateCollections();
Navigator.push(context,
MaterialPageRoute(builder: (context) => ProfDex()));
MaterialPageRoute(builder: (context) {
print(_path);
return ProfDex(path: _path);
})
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
45 changes: 23 additions & 22 deletions lib/profdex.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:io';
import 'dart:math';

import 'package:catch_my_prof/award.dart';
import 'package:catch_my_prof/helpers/imgrotator.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'helpers/names.dart';
Expand All @@ -12,7 +12,9 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ProfDex extends StatefulWidget {
ProfDex({Key key}) : super(key: key);
String path;

ProfDex({Key key, this.path}) : super(key: key);

@override
_ProfDexState createState() => _ProfDexState();
Expand All @@ -22,6 +24,7 @@ class _ProfDexState extends State<ProfDex> {
List<String> profNames = PROF_NAMES;
List<int> profCollections = Player.getCollections();
List<String> profPaths;
Random rng = Random.secure();

@override
void initState() {
Expand All @@ -30,10 +33,9 @@ class _ProfDexState extends State<ProfDex> {
profPaths = List(profNames.length);
for (var i = 0; i < profNames.length; i++) {
String escName = profNames[i].replaceAll(RegExp(' +'), '_');
getImageDir(escName).then((path) {
setState(() {
profPaths[i] = path;
});
String imgPath = getImageDir(escName);
setState(() {
profPaths[i] = imgPath;
});
}

Expand All @@ -48,7 +50,6 @@ class _ProfDexState extends State<ProfDex> {
sharedPreferences.setInt("profdex", 1);
showAlertDialog(context);
}

}

void showAlertDialog(BuildContext context) {
Expand All @@ -59,10 +60,10 @@ class _ProfDexState extends State<ProfDex> {
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
return Color(0xFF92140C); // Use the component's default.
},
)),
(Set<MaterialState> states) {
return Color(0xFF92140C); // Use the component's default.
},
)),
child: Padding(
padding: EdgeInsets.all(10.0),
child: Text("Sweet!",
Expand Down Expand Up @@ -129,18 +130,14 @@ class _ProfDexState extends State<ProfDex> {
"%)";
}

Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}

Future<String> getImageDir(String name) async {
final path = await _localPath;
String getImageDir(String name) {
final path = widget.path;
final imagePath = '$path/$name';
if (Directory(imagePath).existsSync()) {
return imagePath;
} else {
return null;
Directory(imagePath).createSync();
return imagePath;
}
}

Expand Down Expand Up @@ -204,9 +201,13 @@ class _ProfDexState extends State<ProfDex> {
context,
MaterialPageRoute(
builder: (context) => Award(
url: 'assets/' +
index.toString() +
'.png')));
url: profPaths[index] +
'/' +
rng
.nextInt(profCollections[
index])
.toString() +
'.jpg')));
},
child: ImageRotator(profPaths[index],
profCollections[index], 3, 'jpg')),
Expand Down

0 comments on commit 066f694

Please sign in to comment.