Browse Source

sept10

master
Xsivax 3 years ago
parent
commit
b45e7d5b6f
10 changed files with 50431 additions and 16 deletions
  1. +1
    -1
      README.md
  2. +1
    -4
      src/build_rocket/get.rs
  3. +17
    -8
      src/build_rocket/templates.rs
  4. +2
    -2
      templates/assets/components/greentech-content.html
  5. +530
    -0
      templates/assets/js/PLYLoader.js
  6. +6
    -0
      templates/assets/js/three.min.js
  7. +49861
    -0
      templates/assets/js/three.module.js
  8. +3
    -1
      templates/base.html.tera
  9. +9
    -0
      templates/test_extend.html.tera
  10. +1
    -0
      templates/test_include.html.tera

+ 1
- 1
README.md View File

@ -4,7 +4,7 @@ start docker container: docker-compose up
[To do for me ]
! add dynamic language handling with templates
--> extract JSON data into Rust type => use in Template => use conditionally(Accept-Language-Header)
--> extract JSON data into Rust type(as JSON Object) => use in Template => use conditionally(Accept-Language-Header)
! add form handling
! wasm for Threejs canvas
! login, payment, cart

+ 1
- 4
src/build_rocket/get.rs View File

@ -141,9 +141,6 @@ use crate::build_rocket::{ templates, config };
// Render Template Responder
pub fn test() -> Template {
println!("{}", &config::init_lang());
Template::render("test", templates::MyStruct{
title: "test",
lang: "en",
})
Template::render("test_extend", templates::return_json())
}

+ 17
- 8
src/build_rocket/templates.rs View File

@ -1,22 +1,31 @@
// Serde Data Model conversion
use rocket::serde::{ Serialize };
use serde_json::Value;
use serde_json::json;
use std::error::Error;
use crate ::build_rocket::{ config::LANG };
// Implement serde Serialize to create context
#[derive(Serialize)]
pub struct MyStruct<'a> {
pub title : &'a str,
pub lang: &'a str,
pub struct MyStruct {
pub title : String,
pub lang: String,
pub contents: String,
}
// pub fn get_value<'a>( key: &'a str, lang: &'a str ) -> Option<&'a Value> {
// let translation = LANG.get(key).unwrap();
// Some(translation)
// }
pub fn return_json() -> Result<(), Box<Error>> {
let mystruct = MyStruct {
title : "Test".to_owned(),
lang: "English".to_owned(),
contents: String::from("Those are the contents"),
};
let v = serde_json::to_value(mystruct).unwrap();
Ok(())
}

+ 2
- 2
templates/assets/components/greentech-content.html View File

@ -6,8 +6,8 @@
</div>
<p class="short">Start building your own automatic greenhouse with the IoT.</p>
<div class="image">
<img type="image/webp" src="img/replace.png">
</div>
<!--Threejs Canvas-->
<canvas id = "artifactCanvas"></canvas>
</section>
<section id="about">
<p> Greentech or the use of the IoT in agriculture is increasing but due to high costs and complexity not accessible to everyone.<br>

+ 530
- 0
templates/assets/js/PLYLoader.js View File

@ -0,0 +1,530 @@
import {
BufferGeometry,
FileLoader,
Float32BufferAttribute,
Loader,
LoaderUtils
} from '../../../build/three.module.js';
/**
* Description: A THREE loader for PLY ASCII files (known as the Polygon
* File Format or the Stanford Triangle Format).
*
* Limitations: ASCII decoding assumes file is UTF-8.
*
* Usage:
* const loader = new PLYLoader();
* loader.load('./models/ply/ascii/dolphins.ply', function (geometry) {
*
* scene.add( new THREE.Mesh( geometry ) );
*
* } );
*
* If the PLY file uses non standard property names, they can be mapped while
* loading. For example, the following maps the properties
* diffuse_(red|green|blue) in the file to standard color names.
*
* loader.setPropertyNameMapping( {
* diffuse_red: 'red',
* diffuse_green: 'green',
* diffuse_blue: 'blue'
* } );
*
*/
class PLYLoader extends Loader {
constructor( manager ) {
super( manager );
this.propertyNameMapping = {};
}
load( url, onLoad, onProgress, onError ) {
const scope = this;
const loader = new FileLoader( this.manager );
loader.setPath( this.path );
loader.setResponseType( 'arraybuffer' );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( this.withCredentials );
loader.load( url, function ( text ) {
try {
onLoad( scope.parse( text ) );
} catch ( e ) {
if ( onError ) {
onError( e );
} else {
console.error( e );
}
scope.manager.itemError( url );
}
}, onProgress, onError );
}
setPropertyNameMapping( mapping ) {
this.propertyNameMapping = mapping;
}
parse( data ) {
function parseHeader( data ) {
const patternHeader = /ply([\s\S]*)end_header\r?\n/;
let headerText = '';
let headerLength = 0;
const result = patternHeader.exec( data );
if ( result !== null ) {
headerText = result[ 1 ];
headerLength = new Blob( [ result[ 0 ] ] ).size;
}
const header = {
comments: [],
elements: [],
headerLength: headerLength,
objInfo: ''
};
const lines = headerText.split( '\n' );
let currentElement;
function make_ply_element_property( propertValues, propertyNameMapping ) {
const property = { type: propertValues[ 0 ] };
if ( property.type === 'list' ) {
property.name = propertValues[ 3 ];
property.countType = propertValues[ 1 ];
property.itemType = propertValues[ 2 ];
} else {
property.name = propertValues[ 1 ];
}
if ( property.name in propertyNameMapping ) {
property.name = propertyNameMapping[ property.name ];
}
return property;
}
for ( let i = 0; i < lines.length; i ++ ) {
let line = lines[ i ];
line = line.trim();
if ( line === '' ) continue;
const lineValues = line.split( /\s+/ );
const lineType = lineValues.shift();
line = lineValues.join( ' ' );
switch ( lineType ) {
case 'format':
header.format = lineValues[ 0 ];
header.version = lineValues[ 1 ];
break;
case 'comment':
header.comments.push( line );
break;
case 'element':
if ( currentElement !== undefined ) {
header.elements.push( currentElement );
}
currentElement = {};
currentElement.name = lineValues[ 0 ];
currentElement.count = parseInt( lineValues[ 1 ] );
currentElement.properties = [];
break;
case 'property':
currentElement.properties.push( make_ply_element_property( lineValues, scope.propertyNameMapping ) );
break;
case 'obj_info':
header.objInfo = line;
break;
default:
console.log( 'unhandled', lineType, lineValues );
}
}
if ( currentElement !== undefined ) {
header.elements.push( currentElement );
}
return header;
}
function parseASCIINumber( n, type ) {
switch ( type ) {
case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint':
case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32':
return parseInt( n );
case 'float': case 'double': case 'float32': case 'float64':
return parseFloat( n );
}
}
function parseASCIIElement( properties, line ) {
const values = line.split( /\s+/ );
const element = {};
for ( let i = 0; i < properties.length; i ++ ) {
if ( properties[ i ].type === 'list' ) {
const list = [];
const n = parseASCIINumber( values.shift(), properties[ i ].countType );
for ( let j = 0; j < n; j ++ ) {
list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) );
}
element[ properties[ i ].name ] = list;
} else {
element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type );
}
}
return element;
}
function parseASCII( data, header ) {
// PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format)
const buffer = {
indices: [],
vertices: [],
normals: [],
uvs: [],
faceVertexUvs: [],
colors: []
};
let result;
const patternBody = /end_header\s([\s\S]*)$/;
let body = '';
if ( ( result = patternBody.exec( data ) ) !== null ) {
body = result[ 1 ];
}
const lines = body.split( '\n' );
let currentElement = 0;
let currentElementCount = 0;
for ( let i = 0; i < lines.length; i ++ ) {
let line = lines[ i ];
line = line.trim();
if ( line === '' ) {
continue;
}
if ( currentElementCount >= header.elements[ currentElement ].count ) {
currentElement ++;
currentElementCount = 0;
}
const element = parseASCIIElement( header.elements[ currentElement ].properties, line );
handleElement( buffer, header.elements[ currentElement ].name, element );
currentElementCount ++;
}
return postProcess( buffer );
}
function postProcess( buffer ) {
let geometry = new BufferGeometry();
// mandatory buffer data
if ( buffer.indices.length > 0 ) {
geometry.setIndex( buffer.indices );
}
geometry.setAttribute( 'position', new Float32BufferAttribute( buffer.vertices, 3 ) );
// optional buffer data
if ( buffer.normals.length > 0 ) {
geometry.setAttribute( 'normal', new Float32BufferAttribute( buffer.normals, 3 ) );
}
if ( buffer.uvs.length > 0 ) {
geometry.setAttribute( 'uv', new Float32BufferAttribute( buffer.uvs, 2 ) );
}
if ( buffer.colors.length > 0 ) {
geometry.setAttribute( 'color', new Float32BufferAttribute( buffer.colors, 3 ) );
}
if ( buffer.faceVertexUvs.length > 0 ) {
geometry = geometry.toNonIndexed();
geometry.setAttribute( 'uv', new Float32BufferAttribute( buffer.faceVertexUvs, 2 ) );
}
geometry.computeBoundingSphere();
return geometry;
}
function handleElement( buffer, elementName, element ) {
if ( elementName === 'vertex' ) {
buffer.vertices.push( element.x, element.y, element.z );
if ( 'nx' in element && 'ny' in element && 'nz' in element ) {
buffer.normals.push( element.nx, element.ny, element.nz );
}
if ( 's' in element && 't' in element ) {
buffer.uvs.push( element.s, element.t );
}
if ( 'red' in element && 'green' in element && 'blue' in element ) {
buffer.colors.push( element.red / 255.0, element.green / 255.0, element.blue / 255.0 );
}
} else if ( elementName === 'face' ) {
const vertex_indices = element.vertex_indices || element.vertex_index; // issue #9338
const texcoord = element.texcoord;
if ( vertex_indices.length === 3 ) {
buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 2 ] );
if ( texcoord && texcoord.length === 6 ) {
buffer.faceVertexUvs.push( texcoord[ 0 ], texcoord[ 1 ] );
buffer.faceVertexUvs.push( texcoord[ 2 ], texcoord[ 3 ] );
buffer.faceVertexUvs.push( texcoord[ 4 ], texcoord[ 5 ] );
}
} else if ( vertex_indices.length === 4 ) {
buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 3 ] );
buffer.indices.push( vertex_indices[ 1 ], vertex_indices[ 2 ], vertex_indices[ 3 ] );
}
}
}
function binaryRead( dataview, at, type, little_endian ) {
switch ( type ) {
// corespondences for non-specific length types here match rply:
case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ];
case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ];
case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ];
case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ];
case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ];
case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ];
case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ];
case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ];
}
}
function binaryReadElement( dataview, at, properties, little_endian ) {
const element = {};
let result, read = 0;
for ( let i = 0; i < properties.length; i ++ ) {
if ( properties[ i ].type === 'list' ) {
const list = [];
result = binaryRead( dataview, at + read, properties[ i ].countType, little_endian );
const n = result[ 0 ];
read += result[ 1 ];
for ( let j = 0; j < n; j ++ ) {
result = binaryRead( dataview, at + read, properties[ i ].itemType, little_endian );
list.push( result[ 0 ] );
read += result[ 1 ];
}
element[ properties[ i ].name ] = list;
} else {
result = binaryRead( dataview, at + read, properties[ i ].type, little_endian );
element[ properties[ i ].name ] = result[ 0 ];
read += result[ 1 ];
}
}
return [ element, read ];
}
function parseBinary( data, header ) {
const buffer = {
indices: [],
vertices: [],
normals: [],
uvs: [],
faceVertexUvs: [],
colors: []
};
const little_endian = ( header.format === 'binary_little_endian' );
const body = new DataView( data, header.headerLength );
let result, loc = 0;
for ( let currentElement = 0; currentElement < header.elements.length; currentElement ++ ) {
for ( let currentElementCount = 0; currentElementCount < header.elements[ currentElement ].count; currentElementCount ++ ) {
result = binaryReadElement( body, loc, header.elements[ currentElement ].properties, little_endian );
loc += result[ 1 ];
const element = result[ 0 ];
handleElement( buffer, header.elements[ currentElement ].name, element );
}
}
return postProcess( buffer );
}
//
let geometry;
const scope = this;
if ( data instanceof ArrayBuffer ) {
const text = LoaderUtils.decodeText( new Uint8Array( data ) );
const header = parseHeader( text );
geometry = header.format === 'ascii' ? parseASCII( text, header ) : parseBinary( data, header );
} else {
geometry = parseASCII( data, parseHeader( data ) );
}
return geometry;
}
}
export { PLYLoader };

+ 6
- 0
templates/assets/js/three.min.js
File diff suppressed because it is too large
View File


+ 49861
- 0
templates/assets/js/three.module.js
File diff suppressed because it is too large
View File


templates/test.html.tera → templates/base.html.tera View File

@ -5,7 +5,9 @@
<title>{{ title }}</title>
</head>
<body>
{%include "test_include" %}
<h1>{{ title }}</h1>
<p>{{ lang }}</p>
<div id ="content">{% block content %}Me too {% endblock content %}
</div>
</body>
</html>

+ 9
- 0
templates/test_extend.html.tera View File

@ -0,0 +1,9 @@
{%extends "base" %}
{% block content %}
<h1>Override !!</h1>
{{super()}}
{% endblock %}

+ 1
- 0
templates/test_include.html.tera View File

@ -0,0 +1 @@
<h1>Included a nav {{ contents }}</h1>

Loading…
Cancel
Save