How does the indexing of the box in this gltf file work? - opengl

So from my understanding if I want to render a box using indexed triangles, I would need 8 vertices (for the 8 corner points) and 36 indices (the box has 6 sides, with 2 triangles per side and 3 indices per triangle, 6*2*3=36).
So consider the gltf file found here. It is a correct file and I can see the right amount of vertices and indices. However the indices are:
[0, 1, 2, 3, 2, 1, 4, 5, 6, 7, 6, 5, 8, 9, 10, 11, 10, 9, 12, 13, 14, 15, 14, 13, 16, 17, 18, 19, 18, 17, 20, 21, 22, 23, 22, 21]
if i read them correctly. I thought these numbers would never rise above 7 (as there are only 8 vertices to index). Did I read the file incorrectly or how does this indexing work?

You did read the file correctly. Except the cube doesn't have 8 vertices. It has 24. This is the case because, apart from storing position data, they also store normals. OpenGL allows for single-indexing, that is positions, normals, tangets etc. cannot be indexed separately. This means that some vertices need to be duplicated, to be able to be indexed properly. This is explained well here.

Related

Return the last k numbers of a list (Python) [duplicate]

I need the last 9 numbers of a list and I'm sure there is a way to do it with slicing, but I can't seem to get it. I can get the first 9 like this:
num_list[0:9]
You can use negative integers with the slicing operator for that. Here's an example using the python CLI interpreter:
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> a[-9:]
[4, 5, 6, 7, 8, 9, 10, 11, 12]
the important line is a[-9:]
a negative index will count from the end of the list, so:
num_list[-9:]
Slicing
Python slicing is an incredibly fast operation, and it's a handy way to quickly access parts of your data.
Slice notation to get the last nine elements from a list (or any other sequence that supports it, like a string) would look like this:
num_list[-9:]
When I see this, I read the part in the brackets as "9th from the end, to the end." (Actually, I abbreviate it mentally as "-9, on")
Explanation:
The full notation is
sequence[start:stop:step]
But the colon is what tells Python you're giving it a slice and not a regular index. That's why the idiomatic way of copying lists in Python 2 is
list_copy = sequence[:]
And clearing them is with:
del my_list[:]
(Lists get list.copy and list.clear in Python 3.)
Give your slices a descriptive name!
You may find it useful to separate forming the slice from passing it to the list.__getitem__ method (that's what the square brackets do). Even if you're not new to it, it keeps your code more readable so that others that may have to read your code can more readily understand what you're doing.
However, you can't just assign some integers separated by colons to a variable. You need to use the slice object:
last_nine_slice = slice(-9, None)
The second argument, None, is required, so that the first argument is interpreted as the start argument otherwise it would be the stop argument.
You can then pass the slice object to your sequence:
>>> list(range(100))[last_nine_slice]
[91, 92, 93, 94, 95, 96, 97, 98, 99]
islice
islice from the itertools module is another possibly performant way to get this. islice doesn't take negative arguments, so ideally your iterable has a __reversed__ special method - which list does have - so you must first pass your list (or iterable with __reversed__) to reversed.
>>> from itertools import islice
>>> islice(reversed(range(100)), 0, 9)
<itertools.islice object at 0xffeb87fc>
islice allows for lazy evaluation of the data pipeline, so to materialize the data, pass it to a constructor (like list):
>>> list(islice(reversed(range(100)), 0, 9))
[99, 98, 97, 96, 95, 94, 93, 92, 91]
The last 9 elements can be read from left to right using numlist[-9:], or from right to left using numlist[:-10:-1], as you want.
>>> a=range(17)
>>> print a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> print a[-9:]
[8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> print a[:-10:-1]
[16, 15, 14, 13, 12, 11, 10, 9, 8]
Here are several options for getting the "tail" items of an iterable:
Given
n = 9
iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Desired Output
[2, 3, 4, 5, 6, 7, 8, 9, 10]
Code
We get the latter output using any of the following options:
from collections import deque
import itertools
import more_itertools
# A: Slicing
iterable[-n:]
# B: Implement an itertools recipe
def tail(n, iterable):
"""Return an iterator over the last *n* items of *iterable*.
>>> t = tail(3, 'ABCDEFG')
>>> list(t)
['E', 'F', 'G']
"""
return iter(deque(iterable, maxlen=n))
list(tail(n, iterable))
# C: Use an implemented recipe, via more_itertools
list(more_itertools.tail(n, iterable))
# D: islice, via itertools
list(itertools.islice(iterable, len(iterable)-n, None))
# E: Negative islice, via more_itertools
list(more_itertools.islice_extended(iterable, -n, None))
Details
A. Traditional Python slicing is inherent to the language. This option works with sequences such as strings, lists and tuples. However, this kind of slicing does not work on iterators, e.g. iter(iterable).
B. An itertools recipe. It is generalized to work on any iterable and resolves the iterator issue in the last solution. This recipe must be implemented manually as it is not officially included in the itertools module.
C. Many recipes, including the latter tool (B), have been conveniently implemented in third party packages. Installing and importing these these libraries obviates manual implementation. One of these libraries is called more_itertools (install via > pip install more-itertools); see more_itertools.tail.
D. A member of the itertools library. Note, itertools.islice does not support negative slicing.
E. Another tool is implemented in more_itertools that generalizes itertools.islice to support negative slicing; see more_itertools.islice_extended.
Which one do I use?
It depends. In most cases, slicing (option A, as mentioned in other answers) is most simple option as it built into the language and supports most iterable types. For more general iterators, use any of the remaining options. Note, options C and E require installing a third-party library, which some users may find useful.

how to sort out values in a certain time period in R?

I´ve got a time series of temperature data with some wrong values, I want to sort out. The problem is, I want to sort out only the points in a certain period of time.
If I sort out the wrong points by their temperature value, ALL of the points of this temperature value are sorted out (through the wohle measuring period)
This is a very easy version of my code (in reality, there are many more values)
laketemperature <- c(15, 14, 14, 12, 11, 9, 9, 8, 6, 4, 15, 14, 3) #only want to sort out the last 14 and 15
out <- c(15, 14)
laketemperature_clean <- laketemperature [- out] # the 15 and 14s at the beginning are sorted out, too :(
I want to have the whole laketemperature-series in the end, only without the second 15.
I already tried with ifelse, but it didn´t work out.

"how to fix MathJax linebreaking?"

I'm using doubleslash(\\) for line-breaking ,the cursor is pointing to the next line but a single slash(\) is appending with my data.
This is the input I am giving:
Find the median of the given data:"\\ "13, 16, 12, 14, 19, 12, 14, 13, 14"
The output is:
Find the median of the given data: \13, 16, 12, 14, 19, 12, 14, 13, 14.
Single slash is appending to the data.
Try using \\\\. Your content management system may be using \ as a special character, an that may turn \\ into \ in the resulting HTML. For example, Markdown usually does that.

To change the node's name to have a unique list python

I have a problem. Should I create a series of graphs with the barabasi_albert_graph function that is in the library called NetworkX in Python; I should bring all nodes of the graph in a list but so that if I have two graphs one with 5 nodes and one with 10 nodes. I would like to have a unique list so made [0,1,2,3,4,5,6,7 , 8,9,10,11,12,13,14]. The first 5 represent those of the first graph and the 10 the others. Instead I find having [0,1,2,3,4,0,1,2,3,4,5,6,7,8,9]. How can I do to have the first list so that for example if I write 11 in G gives me True?
You can change the labels using convert_node_labels_to_integers like this
In [1]: import networkx as nx
In [2]: G = nx.barabasi_albert_graph(10,3)
In [3]: H = nx.convert_node_labels_to_integers(G,first_label=100)
In [4]: G.nodes()
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [5]: H.nodes()
Out[5]: [100, 101, 102, 103, 104, 105, 106, 107, 108, 109]

Fortran using write(6,*) statement

I'm studying Fortran programs.
When I use the write statements below, my code builds fine but it doesn't run as expected, especially READ(6,*).
What could be the problem and how can I fix it? Thanks!
OPEN(UNIT= 5, FILE='inp.dat')
OPEN(UNIT= 10, FILE='apr1400.dat')
OPEN(UNIT= 11, FILE='ulpu2001.dat')
OPEN(UNIT= 12, FILE='aprslice.dat')
OPEN(UNIT= 6, FILE='HEATFX.dat')
OPEN(UNIT= 13, FILE='HEATFX1.dat')
OPEN(UNIT= 14, FILE='HEATFX2.dat')
OPEN(UNIT= 15, FILE='HEATFX3.dat')
OPEN(UNIT= 7, FILE='out.dat')
OPEN(UNIT= 8, FILE='check.dat')
OPEN(UNIT= 9, FILE='checkout.dat')
READ (5, *)IPLANT
IF(IPLANT.EQ.1)IIP=10
IF(IPLANT.EQ.2)IIP=11
IF(IPLANT.EQ.3)IIP=12
READ (IIP, 250) TITLE
250 FORMAT(A20)
READ (IIP, 300) ISLICE
300 FORMAT(I1)
READ (IIP, 400) RADIUS, XLCYL, DIACYL, DEPTH, GAP, AINLET
IF(ISLICE.EQ.0)READ (IIP, 400) POWER
READ (6, *)HEATFX
IF(HEATFX.EQ.1)llk=13
IF(HEATFX.EQ.2)llk=14
IF(HEATFX.EQ.3)llk=15
READ(llk, 400) HEATFX
READ (IIP, 400) PSYS
READ (IIP, 400) DTSUBI
READ (IIP, 400) XKLOSSI, XKLOSSC
READ (IIP, 405) IPARA
Unit numbers 0, 5, and 6 are associated with the standard error, standard input, and standard output files
Unit 6 is a special one, that might well be the problem here.
In general, try to use larger numbers for file units. I typically use 100, 101, 102, etc.