Gouraud-shaded triangle with Cairo - c

I am trying to get a Gouraud shaded triangle with Cairo using the method explained here.
I have the following code:
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 240);
cairo_t *cr = cairo_create(surface);
cairo_pattern_t * pattern = cairo_pattern_create_mesh();
cairo_mesh_pattern_begin_patch (pattern);
cairo_mesh_pattern_move_to (pattern, 100, 100);
cairo_mesh_pattern_line_to (pattern, 130, 130);
cairo_mesh_pattern_line_to (pattern, 130, 70);
cairo_mesh_pattern_set_corner_color_rgb(pattern, 0, 1, 0, 0);
cairo_mesh_pattern_set_corner_color_rgb(pattern, 1, 0, 1, 0);
cairo_mesh_pattern_set_corner_color_rgb(pattern, 2, 0, 0, 1);
cairo_mesh_pattern_end_patch(pattern);
cairo_set_source(cr, pattern);
cairo_fill(cr);
cairo_surface_write_to_png (surface,"test.png");
However the test.png is just black.

cairo_mesh_pattern_line_to() does not create geometry where actual drawing will happen. It only specifies where the pattern will happen. Thus, to actually see something, you have to specify geometry with e.g. cairo_line_to().
So this should work:
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 240);
cairo_t *cr = cairo_create(surface);
cairo_pattern_t * pattern = cairo_pattern_create_mesh();
cairo_mesh_pattern_begin_patch (pattern);
cairo_mesh_pattern_move_to (pattern, 100, 100);
cairo_mesh_pattern_line_to (pattern, 130, 130);
cairo_mesh_pattern_line_to (pattern, 130, 70);
cairo_mesh_pattern_set_corner_color_rgb(pattern, 0, 1, 0, 0);
cairo_mesh_pattern_set_corner_color_rgb(pattern, 1, 0, 1, 0);
cairo_mesh_pattern_set_corner_color_rgb(pattern, 2, 0, 0, 1);
cairo_mesh_pattern_end_patch(pattern);
cairo_set_source(cr, pattern);
cairo_move_to(cr, 100, 100);
cairo_line_to(cr, 130, 130);
cairo_line_to(cr, 130, 70);
cairo_fill(cr);
cairo_surface_write_to_png (surface,"test.png");

Related

How to import a material-ui slider with react in a web page?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Point Lighting</title>
<script src="https://unpkg.com/react#17/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom#17/umd/react-dom.development.js" crossorigin></script><script src="https://unpkg.com/#material-ui/core#4.11.0/umd/material-ui.production.min.js" crossorigin="anonymous"></script>
<style>
canvas {
width: 40vw;
height: 40vh;
background-color: lightpink;
}
#uiContainer {
top: 0;
width: 80%;
border: 2px solid blue;
background-color: lightblue;
padding: 10px;
float: left;
}
#ui {
background-color: coral;
transform: translate(0%, 0px);
display: block;
float: left;
}
</style>
<h3>
Drag slider to rotate
</h3>
</head>
<body>
<p>ok</p>
<canvas id="canvas" width="200" height="200"></canvas>
<div id="uiContainer">
<div id="ui">
</div>
</div>
<!-- vertex shader -->
<script id="3d-vertex-shader" type="x-shader/x-vertex">
attribute vec4 a_position;
attribute vec3 a_normal;
uniform vec3 u_lightWorldPosition;
uniform mat4 u_world;
uniform mat4 u_worldViewProjection;
uniform mat4 u_worldInverseTranspose;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
void main() {
// Multiply the position by the matrix.
gl_Position = u_worldViewProjection * a_position;
// orient the normals and pass to the fragment shader
// v_normal = mat3(u_worldInverseTranspose) * a_normal;
v_normal = mat3(u_world) * a_normal;
// compute the world position of the surfoace
vec3 surfaceWorldPosition = (u_world * a_position).xyz;
// compute the vector of the surface to the light
// and pass it to the fragment shader
v_surfaceToLight = u_lightWorldPosition - surfaceWorldPosition;
}
</script>
<!-- fragment shader -->
<script id="3d-fragment-shader" type="x-shader/x-fragment">
precision mediump float;
// Passed in from the vertex shader.
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
uniform vec4 u_color;
void main() {
// because v_normal is a varying it's interpolated
// so it will not be a unit vector. Normalizing it
// will make it a unit vector again
vec3 normal = normalize(v_normal);
vec3 surfaceToLightDirection = normalize(v_surfaceToLight);
float light = dot(normal, surfaceToLightDirection);
gl_FragColor = u_color;
// Lets multiply just the color portion (not the alpha)
// by the light
gl_FragColor.rgb *= light;
}
</script>
<script>
function RadToDegSlider(props) {
const {min, max, value, onChange} = props;
const [v, setV] = React.useState(radToDeg(value));
return (
<RangeSlider
min={min}
max={max}
value={v}
onChange={(e, v) => {
setV(v);
onChange(degToRad(v));
}}
/>
);
}
// Setup a ui.
const domContainer = document.querySelector('#ui');
ReactDOM.render(
<RadToDegSlider
value={getRotation()}
onChange={v => {
fRotationRadians = v;
drawScene();
}}
min={-360}
max={360} />, domContainer);
function getRotation() {
return fRotationRadians;
}
function updateRotation(value) {
fRotationRadians = value;
drawScene();
}
</script>
<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
<script src="https://webglfundamentals.org/webgl/resources/m4.js"></script>
<script>
"use strict";
function main() {
// Get A WebGL context
/** #type {HTMLCanvasElement} */
var canvas = document.getElementById("canvas");
var gl = canvas.getContext("webgl");
if (!gl) {
return;
}
// setup GLSL program
var program = webglUtils.createProgramFromScripts(gl, ["3d-vertex-shader", "3d-fragment-shader"]);
// look up where the vertex data needs to go.
var positionLocation = gl.getAttribLocation(program, "a_position");
var normalLocation = gl.getAttribLocation(program, "a_normal");
// lookup uniforms
var worldViewProjectionLocation = gl.getUniformLocation(program, "u_worldViewProjection");
var worldInverseTransposeLocation = gl.getUniformLocation(program, "u_worldInverseTranspose");
var colorLocation = gl.getUniformLocation(program, "u_color");
var lightWorldPositionLocation = gl.getUniformLocation(program, "u_lightWorldPosition");
var worldLocation = gl.getUniformLocation(program, "u_world");
// Create a buffer to put positions in
var positionBuffer = gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Put geometry data into buffer
setGeometry(gl);
// Create a buffer to put normals in
var normalBuffer = gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = normalBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
// Put normals data into buffer
setNormals(gl);
function radToDeg(r) {
return r * 180 / Math.PI;
}
function degToRad(d) {
return d * Math.PI / 180;
}
var fieldOfViewRadians = degToRad(50);
var fRotationRadians = 0;
drawScene();
// Draw the scene.
function drawScene() {
// webglUtils.resizeCanvasToDisplaySize(gl.canvas);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
// console.log(gl.canvas.width,gl.canvas.height);
// Clear the canvas AND the depth buffer.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Turn on culling. By default backfacing triangles
// will be culled.
//gl.enable(gl.CULL_FACE);
// Enable the depth buffer
gl.enable(gl.DEPTH_TEST);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
// Turn on the position attribute
gl.enableVertexAttribArray(positionLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Tell the position attribute how to get data out of positionBuffer (ARRAY_BUFFER)
var size = 3; // 3 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var normalize = false; // don't normalize the data
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer( positionLocation, size, type, normalize, stride, offset);
// Turn on the normal attribute
gl.enableVertexAttribArray(normalLocation);
// Bind the normal buffer.
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
// Tell the attribute how to get data out of normalBuffer (ARRAY_BUFFER)
var size = 3; // 3 components per iteration
var type = gl.FLOAT; // the data is 32bit floating point values
var normalize = false; // normalize the data (convert from 0-255 to 0-1)
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer( normalLocation, size, type, normalize, stride, offset);
// Compute the projection matrix
var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
var zNear = 1;
var zFar = 2000;
var projectionMatrix = m4.perspective(fieldOfViewRadians, aspect, zNear, zFar);
// Compute the camera's matrix
var camera = [100, 150, 200];
var target = [0, 35, 0];
var up = [0, 1, 0];
var cameraMatrix = m4.lookAt(camera, target, up);
// Make a view matrix from the camera matrix.
var viewMatrix = m4.inverse(cameraMatrix);
// Compute a view projection matrix
var viewProjectionMatrix = m4.multiply(projectionMatrix, viewMatrix);
// Draw a F at the origin
var worldMatrix = m4.yRotation(fRotationRadians);
//console.log('stampa');
//console.log(worldMatrix);
// Multiply the matrices.
var worldViewProjectionMatrix = m4.multiply(viewProjectionMatrix, worldMatrix);
var worldInverseMatrix = m4.inverse(worldMatrix);
//console.log(worldInverseMatrix);
var worldInverseTransposeMatrix = m4.transpose(worldInverseMatrix);
//console.log(worldInverseTransposeMatrix);
// Set the matrices
gl.uniformMatrix4fv(worldViewProjectionLocation, false, worldViewProjectionMatrix);
gl.uniformMatrix4fv(worldInverseTransposeLocation, false, worldInverseTransposeMatrix);
gl.uniformMatrix4fv(worldLocation, false, worldMatrix);
// Set the color to use
gl.uniform4fv(colorLocation, [0.2, 1, 0.2, 1]); // green
// set the light position
gl.uniform3fv(lightWorldPositionLocation, [20, 30, 60]);
// Draw the geometry.
var primitiveType = gl.TRIANGLES;
var offset = 0;
var count = 16 * 6;
gl.drawArrays(primitiveType, offset, count);
}
}
// Fill the buffer with the values that define a letter 'F'.
function setGeometry(gl) {
var positions = new Float32Array([
// left column front
0, 0, 0,
0, 150, 0,
30, 0, 0,
0, 150, 0,
30, 150, 0,
30, 0, 0,
// top rung front
30, 0, 0,
30, 30, 0,
100, 0, 0,
30, 30, 0,
100, 30, 0,
100, 0, 0,
// middle rung front
30, 60, 0,
30, 90, 0,
67, 60, 0,
30, 90, 0,
67, 90, 0,
67, 60, 0,
// left column back
0, 0, 30,
30, 0, 30,
0, 150, 30,
0, 150, 30,
30, 0, 30,
30, 150, 30,
// top rung back
30, 0, 30,
100, 0, 30,
30, 30, 30,
30, 30, 30,
100, 0, 30,
100, 30, 30,
// middle rung back
30, 60, 30,
67, 60, 30,
30, 90, 30,
30, 90, 30,
67, 60, 30,
67, 90, 30,
// top
0, 0, 0,
100, 0, 0,
100, 0, 30,
0, 0, 0,
100, 0, 30,
0, 0, 30,
// top rung right
100, 0, 0,
100, 30, 0,
100, 30, 30,
100, 0, 0,
100, 30, 30,
100, 0, 30,
// under top rung
30, 30, 0,
30, 30, 30,
100, 30, 30,
30, 30, 0,
100, 30, 30,
100, 30, 0,
// between top rung and middle
30, 30, 0,
30, 60, 30,
30, 30, 30,
30, 30, 0,
30, 60, 0,
30, 60, 30,
// top of middle rung
30, 60, 0,
67, 60, 30,
30, 60, 30,
30, 60, 0,
67, 60, 0,
67, 60, 30,
// right of middle rung
67, 60, 0,
67, 90, 30,
67, 60, 30,
67, 60, 0,
67, 90, 0,
67, 90, 30,
// bottom of middle rung.
30, 90, 0,
30, 90, 30,
67, 90, 30,
30, 90, 0,
67, 90, 30,
67, 90, 0,
// right of bottom
30, 90, 0,
30, 150, 30,
30, 90, 30,
30, 90, 0,
30, 150, 0,
30, 150, 30,
// bottom
0, 150, 0,
0, 150, 30,
30, 150, 30,
0, 150, 0,
30, 150, 30,
30, 150, 0,
// left side
0, 0, 0,
0, 0, 30,
0, 150, 30,
0, 0, 0,
0, 150, 30,
0, 150, 0]);
// Center the F around the origin and Flip it around. We do this because
// we're in 3D now with and +Y is up where as before when we started with 2D
// we had +Y as down.
// We could do by changing all the values above but I'm lazy.
// We could also do it with a matrix at draw time but you should
// never do stuff at draw time if you can do it at init time.
var matrix = m4.xRotation(Math.PI);
matrix = m4.translate(matrix, -50, -75, -15);
for (var ii = 0; ii < positions.length; ii += 3) {
var vector = m4.transformPoint(matrix, [positions[ii + 0], positions[ii + 1],
positions[ii + 2], 1]);
positions[ii + 0] = vector[0];
positions[ii + 1] = vector[1];
positions[ii + 2] = vector[2];
}
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
}
function setNormals(gl) {
var normals = new Float32Array([
// left column front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
// top rung front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
// middle rung front
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
// left column back
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
// top rung back
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
// middle rung back
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
// top
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
// top rung right
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// under top rung
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
// between top rung and middle
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// top of middle rung
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
// right of middle rung
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// bottom of middle rung.
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
// right of bottom
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
// bottom
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
// left side
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0]);
gl.bufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW);
}
main();
</script>
</body>
</html>
I have all the code in this html file and I import the libraries with cdn, but I can't see the slider in the web page. I'm new to react and webgl, and don't understand where the problem is. How can I do? Thank you all.
There are few issues in your code, I've created a working demo. https://codesandbox.io/s/eager-rgb-9h18u?file=/index.html
You have missed babel import
Missing type="text/babel" on script tag
Slider is the correct component name

how to extract data, convert it to ascii and push it in a list in javascript

I receive data from an external device in decimal values, that I need to convert in ascii and then push it in a list
data example:
#onData: 1,3,200,
78,69,84,71,69,65,82,45,71,117,101,115,116,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70,114,105,103,111,109,97,116,45,49,50,51,52,53,54,55,56,45,87,73,70,73,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,45,71,85,69,83,84,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,123
The first 3 elements of #onData need to be ignored and the last 2 elements as well.
In ascii it would be:
NETGEAR-Guest
Frigomat-12345678-WIFI
MICRO-SYSTEMS
MICRO-SYSTEMS-GUEST
MICRO-SYSTEMS
I have a method to convert:
arrayBuffer2str(buf) {
var str = "";
var ui8 = new Uint8Array(buf);
for (var i = 0; i < ui8.length; i++) {
str = str + String.fromCharCode(ui8[i]);
}
return str;
}
I would need to get rid of the zeros and then push it in an array of strings:
this.wifiNetworks.push("Network-1");
Thank you
If you want to extract the buffer between two zeros and isolate the significant response, try this
let prevVal;
let phrase= [];
for (let i=0; i<array.length; i++) {
let val = array[i];
if (val == 0 && prevVal != 0) {
// end of response
process(phrase);
}
if (prevVal == 0 && val != 0) {
// start new response
phrase= [];
}
phrase.push(val);
prevVal = val;
}
function process(phrase) {
// TODO Process your phrase here and remove the inner zeros
}
Also, consider removing your zeros using the .filter function
Try this,
Assuming that we are getting the input as an array.
const inputArray = [1, 3, 200,
78, 69, 84, 71, 69, 65, 82, 45, 71, 117, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70, 114, 105, 103, 111, 109, 97, 116, 45, 49, 50, 51, 52, 53, 54, 55, 56, 45, 87, 73, 70, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 45, 71, 85, 69, 83, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 123];
const resultArray = [];
let stringIndex = 0;
for (i = 3; i <= inputArray.length - 3; i++) {
if (inputArray[i] !== 0) {
resultArray[stringIndex] = resultArray[stringIndex]
? resultArray[stringIndex].concat(String.fromCharCode(inputArray[i]))
: ''.concat(String.fromCharCode(inputArray[i]));
} else if (inputArray[i - 1] !== 0) {
stringIndex = stringIndex + 1
}
}
console.log(resultArray);
// Add code to push the result to server,
The output would be
[ 'NETGEAR-Guest', 'Frigomat-12345678-WIFI', 'MICRO-SYSTEMS',
'MICRO-SYSTEMS-GUEST', 'MICRO-SYSTEMS' ]

Transformation of an array yields an empty placeholder

I have a numpy array called new_input_processed. The code below transforms it into a one hot array of type float32 (cf byte_list). But when I type byte_list to see the values of this array, I get an empty tensor. I would like to have a non-empty tensor instead. Is it possible ?
In [30]: new_input_processed
Out[30]:
array([[ 83, 111, 109, 101, 32, 83, 101, 113, 117, 101, 110, 99, 101,
32, 111, 102, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
In [31]: byte_list = tf.cast(tf.one_hot(new_input_processed, 256, 1, 0), dtype=tf.float32)
In [32]: byte_list
Out[32]: <tf.Tensor 'Cast_2:0' shape=(1, 100, 256) dtype=float32>
You are not getting an empty tensor. The Tensor object info is returned properly with:
<tf.Tensor 'Cast_2:0' shape=(1, 100, 256) dtype=float32>
Look at the shape, it is just as expected.
Nevertheless, if you want to see the content (i.e. the actual value of the byte_list Tensor object), one way is to call eval().
Something like this should do:
import numpy as np
import tensorflow as tf
new_input_processed = np.array([[ 83, 111, 109, 101, 32, 83, 101, 113, 117, 101, 110, 99, 101,
32, 111, 102, 32, 99, 104, 97, 114, 97, 99, 116, 101, 114,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=np.uint8)
byte_list = tf.cast(tf.one_hot(new_input_processed, 256, 1, 0), dtype=tf.float32)
with tf.Session() as sess: print(byte_list.eval()) # here
Output:
[[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[1. 0. 0. ... 0. 0. 0.]
[1. 0. 0. ... 0. 0. 0.]
[1. 0. 0. ... 0. 0. 0.]]]

how to declare a constant variable from a static variable in C?

I want to get a constant variable from this static variable.
#define video_mode_count 12
static freenect_frame_mode supported_video_modes[video_mode_count] = {
// reserved, resolution, format, bytes, width, height, data_bits_per_pixel, padding_bits_per_pixel, framerate, is_valid
{MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_RGB), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_RGB}, 1280*1024*3, 1280, 1024, 24, 0, 10, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_RGB}, 640*480*3, 640, 480, 24, 0, 30, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_BAYER), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_BAYER}, 1280*1024, 1280, 1024, 8, 0, 10, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_BAYER), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_BAYER}, 640*480, 640, 480, 8, 0, 30, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_8BIT), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_8BIT}, 1280*1024, 1280, 1024, 8, 0, 10, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_8BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_8BIT}, 640*488, 640, 488, 8, 0, 30, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_10BIT), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_10BIT}, 1280*1024*2, 1280, 1024, 10, 6, 10, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_10BIT), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_10BIT}, 640*488*2, 640, 488, 10, 6, 30, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_HIGH, FREENECT_VIDEO_IR_10BIT_PACKED), FREENECT_RESOLUTION_HIGH, {FREENECT_VIDEO_IR_10BIT_PACKED}, 1280*1024*10/8, 1280, 1024, 10, 0, 10, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_IR_10BIT_PACKED), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_IR_10BIT_PACKED}, 640*488*10/8, 640, 488, 10, 0, 30, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_YUV_RGB), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_YUV_RGB}, 640*480*3, 640, 480, 24, 0, 15, 1 },
{MAKE_RESERVED(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_YUV_RAW), FREENECT_RESOLUTION_MEDIUM, {FREENECT_VIDEO_YUV_RAW}, 640*480*2, 640, 480, 16, 0, 15, 1 },};
Now I need to write something
const FREENECT_VIDEO_RGB = [ An instance in the type of freenect_frame_mode ]
How to declare a const var in that format?
The original codes from:
https://github.com/OpenKinect/libfreenect/blob/master/src/cameras.c#L42
And I need to pass a const var freenect_frame_mode into this, and see if it returns -1 or not:
https://github.com/OpenKinect/libfreenect/blob/master/src/cameras.c#L1152
int freenect_set_video_mode(freenect_device* dev, const freenect_frame_mode mode)
{
freenect_context *ctx = dev->parent;
if (dev->video.running) {
FN_ERROR("Tried to set video mode while stream is active\n");
return -1;
}
// Verify that the mode passed in is actually in the supported mode list
int found = 0;
int i;
for(i = 0 ; i < video_mode_count; i++) {
if (supported_video_modes[i].reserved == mode.reserved) {
found = 1;
break;
}
}
if (!found) {
FN_ERROR("freenect_set_video_mode: freenect_frame_mode provided is invalid\n");
return -1;
}
freenect_resolution res = RESERVED_TO_RESOLUTION(mode.reserved);
freenect_video_format fmt = (freenect_video_format)RESERVED_TO_FORMAT(mode.reserved);
dev->video_format = fmt;
dev->video_resolution = res;
// Now that we've changed video format and resolution, we need to update
// registration tables.
freenect_fetch_reg_info(dev);
return 0;
}
I am just stuck with using the function now.
Thanks!
Well, if you want to get an element of an array, you can use the standard syntax array[index].
const freenect_frame_mode FREENECT_VIDEO_RGB = supported_video_modes[5];
for example.
You need to show more code, i.e. the definitions of these symbols, if you want more info.

How to sum columns with same date in Ruby

I have an array with these type of data inside it, and I need to sum up the columns with same date.
[["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]]
How can I do it in ruby? I meant to sum up the rows with the same date into one row, and if there is no duplicated date, keep the old ones.
require 'pp'
require 'matrix'
d = [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]]
pp(
d.group_by(&:first).values.reject do |v|
v.size <= 1
end.map do |e|
e.inject do |m, e|
(Vector.[](*m) + Vector.[](*e)).to_a
end
end
)
Update after comments:
d.group_by(&:first).values.map do |e|
e.inject do |m, e|
[e[0], (Vector.[](*m[1..-1]) + Vector.[](*e[1..-1])).to_a].flatten
end
end.sort
Specification change alert:
def v m
Vector.[](*m.drop(1))
end
d.group_by(&:first).values.map do |group|
r = group.inject do |m, e|
[e[0], *(v(m) + v(e)).to_a]
end
r[1] /= group.size
r[2] /= group.size
r
end.sort
Note. I'm not saying this is homework, but in the cases that are, it should be obvious that when we just do it for the students, we are not really doing them any favors, right? Plus, this solution is provided on a public site that is instantly indexed by google and, being in the top 100 sites in the world, it is not exactly a secret to the prof or the grader. And what if the school is using a national database like http://turnitin.com/ ? I suppose they could check public code snippets if they wanted to. And finally, there is some rather well-written code posted on SO by the, ahem, hobbyists. I'm not sure it can typically pass for lower-division intro-course original work, if I, ahem, say so myself. :-)
aggregated_rows = rows.group_by(&:first).map do |date, rows_by_date|
values = rows_by_date.transpose.drop(1).map { |xs| xs.reduce(:+) }
[date, values]
end
#[["01-04-2013", [100.0, 110.0, 120, 0, 0, 0]],
# ["02-04-2013", [200.0, 250.0, 130, 70, 0, 0]],
...
# ["18-04-2013", [100.0, 130.0, 0, 0, 0, 100]]]
a = [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]]
require "pp"
def group_and_sum_rows_by_date_string(a)
# instantiate a hash that returns an empty array for a key
# that doesn't exist
h = Hash.new([])
a.each do |row|
# populate the hash with date string as key, and array of
# arrays of the values for that date string
h[k=row.shift] = ([row] + h[k]).compact
end
# add up all the corresponding values in each element's array
# arrays, and return the result as an array
h.map{|k, v| [k, v.transpose.map{|x| x.inject(:+)}]}
end
pp group_and_sum_rows_by_date_string(a)
[["15-04-2013", [100.0, 130.0, 0, 0, 0, 17]],
["03-04-2013", [100.0, 110.0, 120, 0, 0, 0]],
["02-04-2013", [200.0, 250.0, 130, 70, 0, 0]],
["17-04-2013", [100.0, 130.0, 0, 0, 30, 90]],
["18-04-2013", [100.0, 130.0, 0, 0, 0, 100]],
["09-04-2013", [0.0, 0.0, 0, 0, 130, 0]],
["01-04-2013", [100.0, 110.0, 120, 0, 0, 0]],
["12-04-2013", [100.0, 140.0, 0, 120, 0, 0]],
["10-04-2013", [200.0, 250.0, 100, 100, 0, 0]],
["11-04-2013", [100.0, 140.0, 0, 110, 0, 0]]]
require 'pp'
a = [["01-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["02-04-2013", 100.0, 110.0, 130, 0, 0, 0], ["03-04-2013", 100.0, 110.0, 120, 0, 0, 0], ["10-04-2013", 100.0, 110.0, 100, 0, 0, 0], ["02-04-2013", 100.0, 140.0, 0, 70, 0, 0], ["10-04-2013", 100.0, 140.0, 0, 100, 0, 0], ["11-04-2013", 100.0, 140.0, 0, 110, 0, 0], ["12-04-2013", 100.0, 140.0, 0, 120, 0, 0], ["09-04-2013", 0.0, 0.0, 0, 0, 130, 0], ["17-04-2013", 0.0, 0.0, 0, 0, 30, 0], ["15-04-2013", 100.0, 130.0, 0, 0, 0, 17], ["17-04-2013", 100.0, 130.0, 0, 0, 0, 90], ["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]]
h = {}
a.group_by(&:first).each{|k,v| v.flatten!.delete(k); h[k] = v.inject(:+)}
pp h
output:
{"01-04-2013"=>330.0,
"02-04-2013"=>650.0,
"03-04-2013"=>330.0,
"10-04-2013"=>650.0,
"11-04-2013"=>350.0,
"12-04-2013"=>360.0,
"09-04-2013"=>130.0,
"17-04-2013"=>350.0,
"15-04-2013"=>247.0,
"18-04-2013"=>330.0}
pp a.group_by(&:first).map{|k,v| v.flatten!.uniq!}
Output:
[["01-04-2013", 100.0, 110.0, 120, 0],
["02-04-2013", 100.0, 110.0, 130, 0, 140.0, 70],
["03-04-2013", 100.0, 110.0, 120, 0],
["10-04-2013", 100.0, 110.0, 100, 0, 140.0],
["11-04-2013", 100.0, 140.0, 0, 110],
["12-04-2013", 100.0, 140.0, 0, 120],
["09-04-2013", 0.0, 0, 130],
["17-04-2013", 0.0, 0, 30, 100.0, 130.0, 90],
["15-04-2013", 100.0, 130.0, 0, 17],
["18-04-2013", 100.0, 130.0, 0, 100]]
pp a.group_by(&:first).map{|k,v| v.transpose.map!{|a| a.inject(:+)}}
Output:
[["01-04-2013", 100.0, 110.0, 120, 0, 0, 0],
["02-04-201302-04-2013", 200.0, 250.0, 130, 70, 0, 0],
["03-04-2013", 100.0, 110.0, 120, 0, 0, 0],
["10-04-201310-04-2013", 200.0, 250.0, 100, 100, 0, 0],
["11-04-2013", 100.0, 140.0, 0, 110, 0, 0],
["12-04-2013", 100.0, 140.0, 0, 120, 0, 0],
["09-04-2013", 0.0, 0.0, 0, 0, 130, 0],
["17-04-201317-04-2013", 100.0, 130.0, 0, 0, 30, 90],
["15-04-2013", 100.0, 130.0, 0, 0, 0, 17],
["18-04-2013", 100.0, 130.0, 0, 0, 0, 100]]

Resources