// File: /workspace/github_cpp_makefile_star2_O3/jingdao[P]PointCloudApp/descriptor.cpp
// Function: Descriptor::kMeansClustering(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>*)
void Descriptor::kMeansClustering(std::vector<std::vector<int>> *indices) {
	int k = 10;
//	int k = (int) sqrt(numPoints / 2);
	//initialize
	for (int i=0;i<k;i++) {
		std::vector<int> v;
		indices->push_back(v);
	}
	int* associations = new int[numPoints]();
	float* means = new float[k * dimension];
	int iterations = 0;

	//choose initial means
	for (int i=0;i<k;i++) {
		int index = rand() % numPoints;
		memcpy(means + i * dimension, data + index * dimension, dimension * sizeof(float));
	}

	bool update = true;
	while (update) {
		update = false;
		for (int i=0;i<k;i++)
			(*indices)[i].clear();

		//assignment step
		for (int i=0;i<numPoints;i++) {
			//find nearest mean
			int minIndex;
			float minDistance;
			for (int j=0;j<k;j++) {
				float distance = 0;
				for (int l=0;l<dimension;l++)
					distance += (data[i*dimension+l] - means[j*dimension+l]) * (data[i*dimension+l] - means[j*dimension+l]);
				if (j==0 || distance < minDistance) {
					minIndex = j;
					minDistance = distance;
				}
			}
			if (minIndex != associations[i])
				update = true;
			associations[i] = minIndex;
			(*indices)[minIndex].push_back(i);
		}

		//update step
		for (int i=0;i<k;i++) {
			for (int j=0;j<dimension;j++) {
				means[i * dimension + j] = 0;
				size_t clusterSize = (*indices)[i].size();
				for (size_t l=0;l<clusterSize;l++) {
					means[i * dimension + j] += data[(*indices)[i][l] * dimension + j];
				}
				means[i * dimension + j] /= clusterSize;
			}
		}
		iterations++;
	}

//	printf("K-Means: Found %lu clusters (%d iterations)\n",indices->size(),iterations);

	delete[] associations;
	delete[] means;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r12
movq %rdi, %r14
movl $0xa, %ebx
leaq 0x10(%rsp), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x7c44
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x973f
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
decl %ebx
jne 0x970c
movq %r12, 0x30(%rsp)
movslq (%r14), %rax
leaq (,%rax,4), %r12
testq %rax, %rax
movq $-0x1, %rbx
cmovsq %rbx, %r12
movq %r12, %rdi
callq 0x2040
xorl %ebp, %ebp
movq %rax, 0x28(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2100
movslq 0x4(%r14), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
leaq (%rcx,%rcx,4), %rdi
cmovsq %rbx, %rdi
callq 0x2040
movq %rax, %r12
callq 0x2080
movl (%r14), %ebx
cltd
idivl %ebx
movslq 0x4(%r14), %r15
movl %r15d, %eax
imull %ebp, %eax
cltq
leaq (%r12,%rax,4), %rdi
imull %r15d, %edx
movslq %edx, %rsi
shlq $0x2, %rsi
addq 0x8(%r14), %rsi
movq %r15, %rdx
shlq $0x2, %rdx
callq 0x2180
incl %ebp
cmpl $0xa, %ebp
jne 0x979c
movq %r12, (%rsp)
movq 0x30(%rsp), %r12
movq (%r12), %rax
movl $0x8, %ecx
movq (%rsp), %r11
movq -0x8(%rax,%rcx), %rdx
cmpq %rdx, (%rax,%rcx)
je 0x97fe
movq %rdx, (%rax,%rcx)
addq $0x18, %rcx
cmpq $0xf8, %rcx
jne 0x97ef
movl $0x0, 0x10(%rsp)
xorl %eax, %eax
xorl %r13d, %r13d
testl %ebx, %ebx
jle 0x9901
movq 0x28(%rsp), %r15
movl 0x4(%r14), %ecx
movl %ecx, %edx
imull %eax, %edx
movslq %edx, %rdx
movslq %ecx, %rsi
shlq $0x2, %rdx
addq 0x8(%r14), %rdx
shlq $0x2, %rsi
movq %r11, %rdi
xorl %r8d, %r8d
movaps %xmm2, %xmm0
xorps %xmm2, %xmm2
testl %ecx, %ecx
jle 0x986f
xorl %r9d, %r9d
movss (%rdx,%r9,4), %xmm1
subss (%rdi,%r9,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm2
incq %r9
cmpq %r9, %rcx
jne 0x9853
testq %r8, %r8
sete %r9b
ucomiss %xmm2, %xmm0
seta %r10b
orb %r9b, %r10b
cmovnel %r8d, %ebp
jne 0x9889
movaps %xmm0, %xmm2
incq %r8
addq %rsi, %rdi
movaps %xmm2, %xmm0
cmpq $0xa, %r8
jne 0x9849
movslq %eax, %rcx
cmpl (%r15,%rcx,4), %ebp
setne %bl
movl %ebp, (%r15,%rcx,4)
movslq %ebp, %rcx
movq (%r12), %rdx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x98cb
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x98e5
leaq 0x10(%rsp), %rdx
movss %xmm2, 0xc(%rsp)
callq 0x8112
movss 0xc(%rsp), %xmm2
movq (%rsp), %r11
orb %bl, %r13b
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
movl (%r14), %ebx
cmpl %ebx, %eax
jl 0x9825
movl 0x4(%r14), %r15d
movq 0x8(%r14), %rax
movl %r15d, %ecx
xorl %edx, %edx
testl %r15d, %r15d
jle 0x99bb
movq 0x30(%rsp), %rsi
movq (%rsi), %rdi
leaq (%rdx,%rdx,2), %r8
movq (%rdi,%r8,8), %rsi
movq 0x8(%rdi,%r8,8), %rdi
movq %rdi, %r8
subq %rsi, %r8
sarq $0x2, %r8
js 0x993e
xorps %xmm0, %xmm0
cvtsi2ss %r8, %xmm0
jmp 0x995a
movq %r8, %r9
shrq %r9
movl %r8d, %r10d
andl $0x1, %r10d
orq %r9, %r10
xorps %xmm0, %xmm0
cvtsi2ss %r10, %xmm0
addss %xmm0, %xmm0
movq (%rsp), %r10
movq %rdx, %r9
imulq %rcx, %r9
cmpq $0x1, %r8
adcq $0x0, %r8
leaq (%r10,%r9,4), %r9
xorl %r10d, %r10d
movl $0x0, (%r9,%r10,4)
xorps %xmm1, %xmm1
cmpq %rsi, %rdi
je 0x99a9
xorl %r11d, %r11d
movl (%rsi,%r11,4), %r12d
imull %r15d, %r12d
addl %r10d, %r12d
movslq %r12d, %r12
addss (%rax,%r12,4), %xmm1
movss %xmm1, (%r9,%r10,4)
incq %r11
cmpq %r11, %r8
jne 0x9987
divss %xmm0, %xmm1
movss %xmm1, (%r9,%r10,4)
incq %r10
cmpq %rcx, %r10
jne 0x9974
incq %rdx
cmpq $0xa, %rdx
jne 0x990a
testb $0x1, %r13b
jne 0x97dd
movq 0x28(%rsp), %rdi
callq 0x2260
movq (%rsp), %rdi
callq 0x2260
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9a0e
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x23a0